4

From what I have read this is how a vector "Size" is set

public Color[] teamAColors = new Color[4];

But when the code is run it looks like this enter image description here

It doesn't seem to matter what number I put for the [4], the Size always stays 6. I am not sure where the 6 number is even coming from as I have not set anything to that number.

I have even tried to

 public Color[] teamAColors;

And then let my array auto populate the Length, but that doesn't change the 6 either.

2 Answers 2

5

Try hitting 'Reset' from the gear-button on the component. It will reset all filled entries, but it should remove the cached size.

I hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

@TimCooley I'm glad I could help. Would you mind selecting my response as the answer (checkmark) please? Thanks and good luck!
4

It's simply

 public Color[] teamAColors;

But what about that "6" value that "won't go away"?

BUT YOU'VE STUMBLED ON TO THE "RESET" GOTCHYA IN UNITY!

enter image description here

Just try it with a different variable name...

public Color[] teste;

See?

enter image description here

There it is, working fine. You can set the size dynamically, so long as you never put the size in the code.

Here is the secret:

Unity "holds on to" serialized values in a complicated way.

If you set the size in code even once, Unity "remembers" this even if you subsequently change the code.

There is a way to reset this "inner" value .. look for the famous tiny reset button.

It is attached to the tiny Cog symbol.

enter image description here

The critical rule to remember is this:

NEVER, EVER, HAVE A DEFAULT VALUE IN CODE FOR PUBLIC VARIABLES IN UNITY!!

public float x;  // do this

public float x=2f;  // generally NEVER DO THIS

While you're at it, here's an incredibly handy trick. Try this ...

[Header("How good is this?")]
public float x;

Here's an even cooler one! try it!

[Range(2f,4f)]
public float x;

5 Comments

Thanks! that did it.
Ah, heh! Here's a good long-winded explanations on the subtlety of that. answers.unity3d.com/answers/254205/view.html Note that also mentions [System.NonSerialized] public which is critical as you move along cheers
alert @TimCooley I packed the answer with more awesome information, be sure to reload. Check out the trick at the end
// generally NEVER DO THIS .. I would actually always do this ... I prefer defaulting my values to something useful and not always to 0 ... There are cases where you don't want to use Range but still allow a user (developer) to even set it to 0 but anyway give it a proper default value
@derHugo for sure, if you're an expert and can avoid the "reset gotchya", you can do as you wish! here, advice for beginners to avoid the "reset gotchya".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.