I’ve been learning/testing a very simple Singleton like manager which works like this:
- Load a new level.
- I got a level0, and then when I press space (whatever) I load next, level1.
- Destroy on level load other copies of similar GameObjects in new scene that is loaded.
In manager I have an OnLevelWasLoaded, which prints something into Debug.Log(“Level init!” + this.gameObject.name).
I can destroy the other copy of manager GameObject and it’s manager script without problem.
However - for some reason manager from first scene runs it’s OnLevelWasLoaded and then the destroyed new level manager does that too (Debug.Log shows this, I print the name of GameObject that does the printing).
I’ve read about this in Unity Answers but there seems to be no solution to this question.
It might be that the whole approach is no good. EDIT: to clarify, it seems that Destroy is only executed after Awake, OnLevelWasLoaded but before rendering or next frame.
Having separate scene with a manager (without anything else) which loads specified level (which of any don’t contain manager copies) works OK.
I’d like to use OnLevelWasLoaded and I’d like to able to “drop” the GameObject+manager into any level and start game from there if possible, without having an empty starter scene containing only manager GameObjects. This might be fine too, but it’s not as neat a solution.
Any tips for better approach and info about OnLevelWasLoaded issue?
Thanks in advance.