I have 2 scenes, “MainMenu”, and “Level1”.
I have an empty GameObject, with a script called “LevelMaster” on it, and in that script is the “DontDestroyOnLoad”.
That all works fine.
However, when I load into the “Level1” scene, none of the scripts will find the “LevelMaster” in thier Start() phase.
For example, there might be an item in the Level1 scene that has this script:
var levelMaster : LevelMaster;
function Start()
{
levelMaster = GameObject.FindWithTag(“LevelMaster”).GetComponent(LevelMaster);
}
Simple enough, has always worked for me in the past- no luck now that I’m bringing the “LevelMaster” along between scenes. Any thoughts?? Assuming I’m missing something simple…can’t get around it though!
Yep, the LevelMaster still is, and I’ve verified it’s the same one…
Currently, I did a hack-job and just dropped a temporary solution into function Update(), it just keeps checking for the LevelMaster every single frame, if it doesn’t already have it (if(!levelMaster) then levelMaster = yada yada)).
Since I have to add that to each and every fracking object that references the Level Master…which is naturally about everything…I’d sure like a proper fix
are you sure the game object is tagged with levelmaster? as you search for a tag.
you can also name the gameobject levelmastergo and search for that name.
edit: and keep in mind that if you return to mainmenu the gameobject will probably be created again so you will have 2 in your game. you must keep proper handling of that case either looking if it is there and only if not create it, or create a second one and destroy one of them manually or move such instaniations stuff in a special scene which is definitely only loaded once in the whole game (right at the beginning).
Ah, I had made various “yield” attempts, but not exactly like that. So that will just pause the script entirely, not even allow it’s function Update to run, till the following line has been satisfied?
the singleton link looks like a good option, something I need to learn regardless. Thanks much!
gw
The yield will simply wait one frame before it’s run, meaning the LevelMaster should have initialised and be present in the scene by the time the execution returns back to that point. Perhaps it’s something to do with mono in that the object is serialized then unserialized between levels, but the unserialized process is done after Start() ???
Wait, that doesn’t sound right. That probably shouldn’t be happening. I’d send that in as repo case via bug reporting just to be safe, but Eric’s co-routine suggestion should work well for now.