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