0
\$\begingroup\$

I'm stuck in this situation:

I have a GameObject with a script, that persist trough Level loads.

Inside this script, when a condition is meet, i Load a Level, and also call a function. That function is a C# Messenger Broadcast() function.

The level that I am loading contain the script with the Listener for that Messenger Broadcast.

The problem is that the Broadcast occurs before the level has been loaded, and so there is no Messenger Listener that can receive the event.

BroadcastException: Broadcasting message OnConnectionError but no listener found.

I need to do the Broadcast only after the level has been loaded, but I don't know how, or maybe there is a better solution.

For a simple example here is the code:

public class NetworkingManager : MonoBehaviour {

    void Start(){
        DontDestroyOnLoad (this);
    }

    void OnConnectionLost(BaseEvent _event ){
        if(Application.loadedLevelName != "LoginScreen"){
            Application.LoadLevel ("LoginScreen");
        }
        Debug.Log ("Disconnected. Reason: " + _event.Params["reason"]);
        Messenger<string>.Broadcast ("OnConnectionError", "Disconnected");
    }
}   
\$\endgroup\$
2
\$\begingroup\$

try using OnLevelWasLoaded :

public class NetworkingManager : MonoBehaviour {
    bool broad = false;   
    void Start(){
        DontDestroyOnLoad (this);
    }

    void OnConnectionLost(BaseEvent _event ){
    broad = true;       
        if(Application.loadedLevelName != "LoginScreen"){
            Application.LoadLevel ("LoginScreen");
        }
    }
    void OnLevelWasLoaded(){
      if (broad ){
        Debug.Log ("Disconnected. Reason: " + _event.Params["reason"]);
        Messenger<string>.Broadcast ("OnConnectionError", "Disconnected");
    broad =false;
      }
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.