- Home /
Corountines and messaging system
Hello, In our project we're using CSharpMessenger and encountered an interesting bug. We're getting an exception when a broadcasted message starts a coroutine. We need coroutines to be able to delay execution of some parts of the code, using WaitForSeconds. When not using messaging system, everything works as expected:
public class GameStateManager : MonoBehaviour {
void Start ()
{
StartCoroutine( StartGameCorountine() ); //Will be executed in parallel
}
private IEnumerator StartGameCorountine()
{
yield return new WaitForSeconds(0.5f);
//Some code to actually start the game
//...
//...
}
}
But when we introduced the messaging system we started getting MissingReferenceException saying:
MissingReferenceException: The object of type 'GameStateManager' has been destroyed but you are still trying to access it.
Here's our code using the messaging system:
public class GameStateManager : MonoBehaviour {
void Start ()
{
Messenger.AddListener("start game", StartGameMessageHandler);
Messenger.Broadcast("start game");
}
void StartGameMessageHandler()
{
StartCoroutine( StartGameCoroutine() ); //The exception rises at this line.
}
private IEnumerator StartGameCoroutine()
{
yield return new WaitForSeconds(0.5f);
//Some code to actually start the game
//...
//...
}
}
Your answer
Follow this Question
Related Questions
WaitForSeconds Not Working 4 Answers
Problem with coroutine 2 Answers
Ienumerator wait for event 0 Answers
yield return waitforseconds not waiting? 3 Answers
SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved) 2 Answers