- Home /
How to set starting order of scripts?
Let's say I have three scripts, each attached to three separate game objects.
In one script I am loading up some data from text files, and that data needs to be available to the other two scripts, as they need use of it in their Start() functions.
Based on the Debug.Log lines I have printed out from each of the scripts, it appears that Unity just sort of arbitrarily loads everything as it feels like it. How can I set the order of script priority so that a certain script ALWAYS loads and finishes before any other scripts in the project do?
I must say, this is terrible design by unity :( it's not even necessary to explain why
@UnitedBluff This is an (answered) question from 5 years ago that's not even an issue...
http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html
I'm sorry, but I don't understand your point. I know you can "control" execution order by putting some code in Awake and some in Start...but that requires that one script "knows" about other scripts..and you can really only control a very few number of scripts initialization order..what if you have 20 scripts that need to be executed in a specific order ? You only have Awake and Start..you would need "Awake1" "Awake2" etc..(again, horrible design.). Why not simply let the scripts initialize in the order they were added to the GameObject. So yes. it is still an issue, unless there are other ways to fix this.
Answer by Eric5h5 · Mar 17, 2010 at 01:31 AM
Use Awake() to initialize stuff that has to be done before Start(). If you need more than two levels of ordering, then you have to implement some kind of routine on a game manager object that calls functions on other scripts in your defined order.
I've been using Awake for this and I had the Debug.Log info printing from WITHIN the functions I was calling with Awake. From that it appeared as is my Awake things were running with everything else. I just put the Debug.Log stuff into the actual Awake function and now I see them print at the beginning, every time.
Thanks for the info and leading me in the right direction.
Answer by Content1of_akind · Apr 25, 2019 at 05:14 PM
https://docs.unity3d.com/Manual/class-MonoManager.html Edit > Player Settings > Script Execution.
Under (Default Time) click on the Plus(+) Sign on the bottom right to add the Script(GameManager Script) that should run before any other Script. The script name doesn't matter, just the concept of which Script you want to run before another. that being said if the script works do not follow these steps at all, your code is fine as is. Only use it if your script is interfering with time order of another Script from performing properly. Which is usually the GameManager and AudioManager I've only came across.
This is what helped my music to start playing again because my GameManager Script and AudioManager Script was loading(running) at the same or wrong time which I guess caused it not to recognize the AudioManager.instance I wrote to the GameManager. Hope this helps you as it did me, Cheers!