Been trying to figure this one out on my own, but I’m having a bit of trouble…
Alright, so here’s the situation -
A new scene is loaded. I have a game object that has a script attached to it. I wanted to check and make sure that the scene was actually fully loaded before running some code, so I created this in the script attached to that game object -
The problem is, CodeToRun does run, the StartCoroutine is executed, but the function that is supposed to run after the delay CodeThatRunsAfterDelay(), is executed right away. No delay at all (The delay is supposed to be 2.5 sec)
well I can’t see nothing bad with it, so it must be something else.
have you tried placing debug.logs in CodeToRun() and SomeCoroutine() to see what happens in what order and how many times? also are you sure that codeDelayTime var is set to 2.5?
Yes, the delay time is 2.5f. I set breakpoints during debugging to see what was happening. The breakpoint for StartCoroutine(SomeCoroutine()); gets hit, and then I hit continue, and then the breakpoint for CodeThatRunsAfterDelay(); is immediately hit after that.
I also ran a Debug.Log("Test") inside where StartCoroutine(SomeCoroutine()); is run to see how many times it was getting called, and the console confirms it’s just once, so StartCoroutine(SomeCoroutine()); is only being called once, once that scene has fully loaded.
EDIT: “also are you sure that codeDelayTime var is set to 2.5?”
That is actually a good point - I’m setting that delay time in Start() (it’s actually a variable). I wonder if Start() is being called after the Coroutine has already started…
EDIT2: Yup. My suspicion was correct. The variable isn’t being assigned a value until after that Coroutine has already started. Damn, that’s actually good info.
Definitely print this value out. I bet it will be zero.
Keep in mind where / when you’re doing this too:
Serialized properties in Unity are initialized as a cascade of possible values, each subsequent value (if present) overwriting the previous value:
what the class constructor makes (either default(T) or else field initializers)
what is saved with the prefab
what is saved with the prefab override(s)/variant(s)
what is saved in the scene and not applied to the prefab
what is changed in Awake(), Start(), or even later etc.
Make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.
Field initializers versus using Reset() function and Unity serialization: