function Start() not finding object that was kept via "DontDestroyOnLoad", any ideas?

Hi all, and thanks for any help as always-

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!

Thanks much!
(Unity 3.5)

Is the LevelMaster object still in the scene hierarchy after you switch levels?

Hi Cameron, thanks for the reply-

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 :stuck_out_tongue:

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).

Hi exiguous-

Good suggestions, I’ve definitely dealt with all that though :frowning: No duplicates allowed here, lol, and the tag is fine.

I know it works in “theory”, because if I hit play in the local level, it all works fine, or if i force it to wait a single frame past “Start”.

Urrg…well, at least my work-a-round is letting me get other code done, for now.

Thanks much!

To make a less hacky hack, use a coroutine:

function Start()
{
    yield;
    levelMaster = GameObject.FindWithTag("LevelMaster").GetComponent (LevelMaster);
}

You could also consider using a singleton, then you wouldn’t have to get a reference.

–Eric

Hey Eric-

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.

No, Update will run. You could do this though:

function Start () {
    enabled = false;
    yield;
    levelMaster = GameObject.FindWithTag("LevelMaster").GetComponent (LevelMaster);
    enabled = true;
}

Then Update won’t run until the script is enabled.

–Eric

LevelMaster.js

var myInstance : LevelMaster;

function Start ()
{
myInstance = gameObject.GetComponent.();
}

public static function GetInstance() : LevelMaster
{
return myInstance;
}

OtherScripts.js

function Start ()
{
levelMaster = LevelMaster.GetInstance();
levelMasterGO = levelMaster.gameObject;
}

This will work as long as there’s only one instance of LevelMaster.js in the whole game.