Unable to change array size in Inspector variable in Unity?
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
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.
Solution 1:
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!
Solution 2:
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!
Just try it with a different variable name...
public Color[] teste;
See?
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.
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;