I would like to run another initialize command after Start() but I do not know the best way to do this. I want to do something like this:
void Awake() {
//Set up the environment
}
void Start() {
// Prepare the variables for the environment
}
void LateStart() {
// Ask the other scripts about their variables
}
I can’t ask the other scripts about their variables until after Start or I will run into a race condition (where whether it errors or not will be based on the sequence the Start() functions are called). I can add a toggle to Update() so the first time it updates it runs the LateStart() commands, but I don’t think this is the best way. Any ideas?
There is a built-in system to control the order in which things are processed:
Edit | Project Settings | Script Execution Order
Here you can adjust each script up or down in a list, so that they process in the specified order and either before or after the usual ‘default time’.
In the OP case it will fix the race conditions provided that none of the references end up being circular (object X has variable y referencing object Y, and object Y has variable x referencing object X). In these cases it is necessary to make a public setter function for one of them and call it from the other.
Unfortunately this feature is pretty much hidden away, so it isn’t a great solution if you intend to share your project at all… Also depending on a little known feature to make your system work feels bad to me, but I think it is less nasty than delayed coroutine initialisation which will probably end up with your Update being called before your LateStart.
Another Way To Do This is…
void Start()
{
StartCoroutine(LateStart("Time You Want To Wait"));
}
IEnumerator LateStart(float waitTime)
{
yield return new WaitForSeconds(waitTime);
//Your Function You Want to Call
}
Hope It Will Helps You…
You could use coroutines in your start method and define the delay you want
In a Coroutine there are also WaitForFixedUpdate or WaitForEndOfFrame that could help you. If you wanna do it time based you could invoke your method. That’d be simpler than a Coroutine in that case.