- Home /
How to initialize after Start()
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?
Answer by Mehul-Rughani · May 22, 2015 at 04:08 AM
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..
using Invoke ins$$anonymous$$d of Coroutine makes more sense here.
Since this thread has already been revived...
You can also use $$anonymous$$ain(). It is automatically called between Awake() and Start().
Cheers, ~LegendBacon
Answer by pjbaron · May 09, 2018 at 02:56 PM
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.
I believe this is the best solution for given problem. Also if anyone is wondering where are these settings saved - it looks like this settings are saved in .meta file of given script.
This should be the correct answer. The root problem is that the scripts are not starting in the correct order. Delaying the execution with a co-routine, as the current selected answer suggests, is a bandaid on top of the actual problem.
Answer by chintan_shroff · May 22, 2015 at 09:20 AM
You could use coroutines in your start method and define the delay you want http://docs.unity3d.com/ScriptReference/Coroutine.html
Answer by gglobensky · Jun 16, 2017 at 02:52 AM
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.