Game Development Reference
In-Depth Information
if (Instance != null && Instance != this)
{
//Destroy other instances if it's not the same
Destroy(gameObject);
}
//Save our current singleton instance
Instance = this;
//Make sure that the instance is not destroyed between
scenes
//(this is optional)
DontDestroyOnLoad(gameObject);
}
This is the same as before but with a little extra debug information so you can see when it
is initialized in the Console window.
Then, we add a method so we can register recipients or subscribers to the messages (with
the associated UnSubscribe and ClearAllSubscribers methods), as follows:
//The Subscribe method for manager
public void Subscribe(Action subscriber)
{
Debug.Log("Subscriber registered");
subscribers.Add(subscriber);
}
//The Unsubscribe method for manager
public void UnSubscribe(Action subscriber)
{
Debug.Log("Subscriber registered");
subscribers.Remove(subscriber);
}
//Clear subscribers method for manager
public void ClearAllSubscribers()
{
Search WWH ::




Custom Search