- Home /
Is there a difference between initializing a variable in the Start() function and initializing a variable globally?
For example, if I initialize a variable globally, it would look like this:
public class ExampleOne : MonoBehaviour
{
public float x = 10.0f;
void Start()
{}
void Update()
{}
}
But if I initialize a variable in the Start() function, it would be like this:
public class ExampleTwo : MonoBehaviour
{
public float x;
void Start()
{
x = 10.0f;
}
void Update()
{}
}
Is there any difference between these two? Because I played around a bit and I noticed that if I initialize a variable to 10 globally, once I enter play mode, the said variable doesn't start off with the value 10 in the first frame.
But if I initialize the variable in the Start() function instead, the variable starts off with the value 10 right from the first frame once I enter play mode.
Any idea why this is the case? Are the statements in Start() functions somehow prioritized over global statements?
Answer by pophead2 · Mar 24, 2018 at 07:40 PM
Yes. If you initialize it in start, it won't be ready, for example, on Awake() or OnEnable() since they are done before Start();
Yeah but I'm only referring to Start() function and global variable. $$anonymous$$y script doesn't have Awake() or OnEnable(). When a code is activated, what happens in the first frame? Are the global variables checked first or is the start function checked first?
Answer by plangiewicz · Mar 24, 2018 at 07:46 PM
if you initialize global float a = 10; then it is initialized before awake(). Difference is if you want to get component then difference is betwen awake() start() onenable etc... because awake is before start and onenable.
If you have
class something{
anotherClass another;
then you should init this another in awake()
Answer by sisse008 · Mar 25, 2018 at 11:08 AM
TheSaviour, you do not create Awake, Enable, and start. they are names of time instances. your Update function is called x FPS. even if you did not have an update function, your game would still run at x FPS.
if you have a function Start() in your script than it is called a short time after your game begins. if you have an Awake() function than it will be called at an even shorter time after your game begins. there for, it is safer.
if a different script in your game is running and that script has
void Awake()
{
x++;
}
then you will get an error because x is being set in the Start() function and start function is called after Awake().
Alright I get it. But what about global variables that I assign before the Start() or Awake() function? When my game starts, are those variables checked before the Awake() or Start() function?
yes. you can do a simple check
int x =1;
void Start()
{
Debug.Log("x = " + x);
}
Answer by Glurth · Mar 25, 2018 at 05:16 PM
When you create a member variable in your Monobehavior class
public int a=1;
The value 1 will be assigned to integer "a" when the object is INSTANTIATED (created / put in the scene). The game need not even be launched (played) for this value to be assigned (unlike Start). As soon as your behavior is put on an object, in the editor, this value will be assigned.
Once the object is instantiated, the value is saved, by unity serializing the Monobehavior object and saving it with the scene. When the scene is re-loaded, this serialized value will be re-assigned to the variable. If the scene is loaded becaue the game has just been started, Start() will be called AFTER the serialized values have been reassigned.
You can optionally use the Reset() function, to assign these values (what I would recommend). This also works in the editor, and does not require the game actually be running. This way when you select "reset" from the object's editor menu, the object ACTS like it has just-now been instantiated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612155310im_/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 to get Unity to ask what project to load every startup? 1 Answer
Making a start screen in Unity 3 Answers
DontDestroyOnLoad() not calling awake/start again 2 Answers
Object reference becomes null between Awake and Start after scene load 2 Answers