OnLevelWasLoaded as a Coroutine problem

Hi,

I have an animated character set to DontDestroyOnLoad. This character has an Animator Controller with multiple variables. When a new level is loaded, for some reason I don’t know, these variables are being reset to default values.

That would be ok, if I could just put OnLevelWasLoaded(int level) { //put all Animator Controller variables back to where they were before }, however, if I do this, variables DO NOT get affected, what I am guessing is that the OnLevelWasLoaded is being called, setting the variables to the right values, AND THEN the Animator Controller is reseting those variables after OnLevelWasLoaded was called.

So the solution I thought of and as described on Unity`s documentation, OnLevelWasLoaded can be a coroutine, so I have the following code:

        private IEnumerator OnLevelWasLoaded(int level)
	{
		print("Level Loaded");
		yield return new WaitForSeconds(0.5f);
		print("Level Loaded after a half sec");
	}

Problem is, the second print never happens. And if I make another method, which is a coroutine, such as:

	private void OnLevelWasLoaded(int level)
	{
		print("Level Loaded");
		StartCoroutine(DelayedOnLevelWasLoaded());
	}
	private IEnumerator DelayedOnLevelWasLoaded()
	{
		yield return new WaitForSeconds(0.5f);
		print("Level Loaded after a sec");
	}

The second print never gets called either! I honestly don’t know what to do… is this a known bug? Is there any other form to adjust the variables like after “start” time on a scene load?

Thanks for the help in advance,
Best regards,
Allan

To be honest: Dont even mess with DontDestroyOnLoad functions and/or OnLevelWasLoaded functions.
I tried for quite some time and your results with this combination are always far from expected (maybe because of some behind the scenes magic, I don’t know).

I’d just save all vars to a file or to playerprefs, load the level, instantiate a new player (here I’d go for an empty with a spawnscript, which is in the start method) and set all vars as they should be.
Despite you don’t run in unexpected problems, you can fully understand and tweak what you are doing and you are not dependent on some scripts, you don’t even know how they should or should not work.

Greetings, Julius

If you just want to have a delay, have you tried Invoke(“DelayedOnLevelWasLoaded”, 0.5f);
It doesn’t need to be a coroutine.

Thanks guys,

I was being a noob and not noticing that OnEnable also gets called again on scene load, so I managed to solve it by switching my code to OnEnable instead of OnLevelWasLoaded… but really, it does NOT work as the documentation says, what I think is happening is that it is called actually BEFORE the scene is changed, and the coroutines just get destroyed as soon as they are created… at least that is what I would guess.

Seems like a bug to me… in my case I dont need to keep activating and deactivating my GO so OnEnable is fine… but if I had to I would be in serious trouble…

Anyway, thanks for the suggestions!