Execution order of Start methods

Hi there,
I understand the Start method in a script will run once in the beginning of the program. But the problem is, there can be many different scripts at a time, so how does Unity decide which Start method from which script should run first? In my case, I want Start A in scriptA to run first and StartB in scriptB to run second because a public variable a in scriptA, which will be assigned a value in StartA, will be used in StartB. But now, every time when I click play, since the order goes another way around, when StartA runs, variable a hasn’t been assigned the correct value yet, therefore I get an error. So the key to solve the issue is to be able to set the execution order manually. So is there a way to do that in Unity?

thanks,
Tim

Hi Tim,

Unity executes the methods in an arbitrary order meaning it executes all Awake methods in an arbitrary order, then all Start methods in an arbitrary order, and so on.

If you need a specific order, consider using the Awake method, which gets executed before the Start method. See here for the predefined execution order of Unity’s script event methods:

If Awake is no solution for your specific problem, you could define an execution order in the Project Settings. See here:


See also:

Thank you, Nina.

Using awake is really convenient. Before I only saw it somewhere and didn’t really know what it could be used for when you had start already. Now I just realized that it could be used for initializing public variables. I don’t know if that’s a common use case for awake, but in my case it fits perfectly well.

I can only speak for myself but I use Awake mainly to initialise things in the same instance. This way, I know that the initialised things already exist when I try to access them in the Start method of another instance.

If I excuted everything in Awake or everything in Start, I would eventually encounter the problem you described in your initial post. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms