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.

Fattie
  • 27,874
  • 70
  • 431
  • 719
Tim Cooley
  • 749
  • 4
  • 19
  • 38

2 Answers2

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!

andeart
  • 935
  • 6
  • 19
  • @TimCooley I'm glad I could help. Would you mind selecting my response as the answer (checkmark) please? Thanks and good luck! – andeart Feb 02 '16 at 23:54
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;
Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Thanks! that did it. – Tim Cooley Feb 02 '16 at 23:52
  • Ah, heh! Here's a good long-winded explanations on the subtlety of that. http://answers.unity3d.com/answers/254205/view.html Note that also mentions `[System.NonSerialized] public` which is critical as you move along cheers – Fattie Feb 02 '16 at 23:56
  • 1
    alert @TimCooley I packed the answer with more awesome information, be sure to reload. Check out the trick at the end – Fattie Feb 03 '16 at 00:00
  • `// 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 May 11 '21 at 06:52
  • @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". – Fattie May 11 '21 at 11:24