Is it a big difference between initializing variable in the beginning of the script - where it was declared, or inside Start() function? In both cases variable will be initialized, when script start working.
Is it just a good tone - to initialize variables inside Start()?
Well, the best answer that I have here is: It depends.
If you have a public variable that you initialize at the start of the script, it will set itself to what you initialized it to in the editor, but then if you change that in the editor, it will have the changed value.
If you initialize variables in Start() you cannot change them in the editor, accidentally or otherwise. (Well, you can - but when you start playing they will always be set to the values you put in your Start() method).
Or in some cases you might even want to initialize them in Awake() (which is an initialize method that takes place before Start() ).
There are more options then proposed for initialisation. They include:
At declaration - Use this to set up a default value. Note that these values can be overridden by the values typed in the inspector. Useful for data you want serialised that needs a default value. For data not serialised there is little difference between initialising here and initialising in any of the built in functions.
Via the inspector - For serialisable types you can mark these as [SerialiseFeild] and initialise them via the inspector. You can also initialise public variables via the inspector, but this can lead to bad coding practice
Awake - Use this for all internal initialisation. Set up all of the internal class variables. Set up references to other classes, but don’t access the other classes yet.
Start - Use this for all external initialisation. Anything that relies on the variables of other classes should be done here.
OnEnable - Use this as a “reset” for any variables that need to be cleared before reusing the object. Great for object pools and the like.
You can Initialize the variables before the inspector is opened by calling the Awake() function from the Editor. You’ll need to define the Initialize() method inside your Monobehavior and execute it from the editor as below using this example. ( you can do the commented out way, which may be better in some cases)
using System.Collections;
using UnityEngine;
using UnityEditor;
//must be placed inside the editor folder
[CustomEditor(typeof(<myComponent>)]
public class myCustomComponentEditor : Editor
{
private void Awake()
{
<myComponent> myTarget = target as <myComponent>;
myTarget.Initialize(); // initialize your variables in your component
//myTarget.myInt=3;
}
}
I have a public variable in a MonoBehaviour that I’m reading in OnCollisionStay(). No matter if I initialize it in the declaration or Awake() or Start(), when OnCollisionStay() is called the value is initialized to the one in the editor field. Also using [HideInInspector] doesn’t help.
I found the only way to initialize it in the code is to make the variable private and to add a setter.