- Home /
What is the difference between a variable instantiated inside a method(specifically inside void Start()) and one not inside any methods?
Just finished a project, and realize I'm confused with these two things:
int life = 10;
and
int life;
void Start(){
life = 10;
}
usually, if one doesn't work as I expected, I just do the other one. I also notice on the tutorials I found that when they declare a List or Script, they always instantiate it inside Start()
List <GameObject> stages;
void Start(){
stages = new List<GameObject>();
}
Hope to know the difference if there are, including on the event when I moved from one scene to another. For example, if I click a button that has a script like this:
OnMouseDown(){
//scene 1 has the int life;
Application.Load("Scene1");
//use static variable Player to access int life;
Player.life = 3;
}
what is the value of life 0, 10 or 3;
Answer by PouletFrit · Jun 08, 2014 at 04:03 PM
Initializing a variable outside of Start or Awake will simply initialize that variable at the moment the constructor for that object is called. If your object inherit from MonoBehaviour the constructor get called when the object is created/loaded. Then once the serialized data is restored (if needed) to the object, Unity goes through Awake function then throught the Start function.
So pretty much it's only a matter of when you want that variable to be initiated.
Constructor (which is kind of hidden in unity) --> Awake --> Start
And life should end up having a value of 10. Unity load the level at the next frame, so it will finish the current function and the Player.life = 3 will be overriden by the Start/Awake functions
but when I put int life = 10 outside start or awake, that is I instantiate it above, then life will be 3, right?
Declare - Assign memory of certain layout (and size).
Instantiate - Assign an initial value.
You may instantiate during declaration if you wish. It will be a default value and is sometimes good practice to avoid nulls and nonsense. Do this outside functions to extend the scope of that variable for use in the whole script.
You may Declare without Instantiating. You are simply reserving a chunk of memory of a certain layout, but not giving it a value. You may then assign a value to this variable anywhere in the script and the var will retain that value through the whole script.
$$anonymous$$ost people perform Instantiation during Start() to ensure that the program has run enough for the relevant objects etc to exist, avoiding nulls. If you Declare during Start() that variable will only have scope inside that function and will not exist outside of that function.
Answer by robusto · Jun 08, 2014 at 03:49 PM
http://unity3d.com/learn/tutorials/modules/beginner/scripting/awake-and-start http://docs.unity3d.com/Manual/ExecutionOrder.html (look at the graph)
Your answer
![](https://koobas.hobune.stream/wayback/20220613151057im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
How do i start a game with Unity, what do i need first? 0 Answers
C# Accessing Variables from Class Variable 1 Answer
C# Referencing a Non-Static Variable Help 2 Answers
How do i access a variable in one script from another script? 2 Answers