sending message to gameobjects of newly loaded level

if (GUI.Button(new Rect(Screen.width/2-50, Screen.height/2+45, 100, 50), "Host A Game"))
        {
            Application.LoadLevel(2);
            Wait();
            Wait();

           foreach (GameObject go in FindObjectsOfType(typeof(GameObject)))
		        go.SendMessage("OnServerStart", SendMessageOptions.DontRequireReceiver); 	
        }

i tried to yields… but that doesnt work, i would like to send this message to all the game objects in the next scene but i get a null reference object of onGUI().

Bump… any ideas on this? it seems to work for network level loading, why cant it work for non network level loading

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html?from=Application

You could try doing it from the other direction.

Interesting. I will try this when i get home… i wonder though if this will be able to iterate through the new levels game objects. IE what is the scope of WasLevelLoaded…

If you have an issue, throw in a yield to skip a frame.

This doesnt work… OnWasLevelLoaded() is called from the newly loaded level everytime… i want to be able to send a message to the new level from the level that loaded it. this is not possible i presume…

mehh… i guess i can just write and read from a file.

It is possible.

The monobehaviour that has the OnLevelWasLoaded in could cache the message you want to send for example (ie the method and parameter - the object logically can’t be cached as it does not exist in the old scene) and then you can do the SendMessage in OnLevelWasLoaded

Certainly possible. However it makes the assumption that you have a game manager singleton in play driving your state machine and deciding when to load a level. Remember that all objects are destroyed when loading a new scene. If the object you’re sending the message from doesn’t exist in the new scene, well it certainly can’t send a message!

If you have a bad design pattern but are happy with it and still want to accomplish this, you could just store the message you want in PlayerPrefs and use OnLevelWasLoaded() or Start() in the new scene to read it. No need to send messages, the objects would automagically read the message themselves.

game manager singleton… i think that is the link missing here… i think i read somewhere that the penelope project does this? If so i’ll check there.

Thanks for the help!

I will look into this… thanks!