Instance Handler
var nm = InstanceHandler.NetworkManager;public class GameManager : NetworkBehaviour
{
private void Awake()
{
//We register the GameManager instance
InstanceHandler.RegisterInstance(this);
}
private void OnDestroy()
{
//Upon being destroyed, we unregister the game manager instance
InstanceHandler.UnregisterInstance<GameManager>();
}
private static void GetInstanceExample()
{
//This will fail if the manager isn't registered
InstanceHandler.GetInstance<GameManager>().Success();
}
private static void TryGetInstanceExample()
{
//This will only run if we get the manager. Potentially invert the if statement and log and error if you don't get it
if(InstanceHandler.TryGetInstance(out GameManager manager))
manager.Success();
}
private void Success()
{
Debug.Log("Now we're logging from the GameManager instance!", this);
}
}Last updated