- Home /
Question by
Curyous · Mar 27, 2012 at 06:46 PM ·
variablesstartinitialisation
Are variables automatically initialised?
When I declare something like:
var myVariable : Vector3;
In Update() will it be null, or have default 0 values for x, y, z?
Do I need to explicitly initialise all variables in Start()?
Comment
Best Answer
Answer by Eric5h5 · Mar 27, 2012 at 07:02 PM
It depends on whether it's a value type or not. Value types such as primitives (float, int, etc.) and structs (Vector3, Color, etc.) have default values and cannot be null. Otherwise, reference type variables (things like classes and arrays) are null unless initialized.
Answer by DaveA · Mar 27, 2012 at 06:51 PM
I believe those do, but it's generally best practice to init just in case, I think. I know Objects, like arrays and such, are not, you'll get null references.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065613im_/https://answers.unity.com/themes/thub/images/avi.jpg)