Array Length is Zero!

Hi
I’ve declared array like this in C#

public float[ ] LastCreationTime= new float[5];

but when i go to start() and check the length of array it says the array length is zero!

print (LastCreationTime.Length);

i’m not sure if i missed something but i think this code is ok and maybe monodevelop has some settings that resets data or something like that?!

EDIT:
because the variable is public the size property of array is set in unity environment to 0 by default and when i changed the size value inside the unity to 5 the problem fixed, but still this question remains:
How can i define and set the size inside the script? or is it possible?

Public variables get their values from the inspector, not your code. If you don’t want that, make the variable private instead.

–Eric

1 Like

thx eric
btw how can i set the question is answered? :slight_smile:

There’s no formal mechanism for that. Informally, it’s traditional to shout “Woo howdy, this’n here question be answered!”, toss your hat, and fire several shots in the air.

–Eric

3 Likes

Put [HideInInspector] attribute on top of the variable. That way, it will be publically accessible, and yet not show on inspector, in which case it’s length would not get overridden by the inspector.

But it will still get serialized, so it doesnt really solve the problem.