- Home /
Can someone help me with instance = this ?
So I have this code that works exactly how I want it to. I needed it to not destroy itself when my character transitions to another scene and to remove duplicates when entering a new scene. My GameManager's purpose is to shut off the player's movement with PlayerMovement.instance.canMove = false; so that the player would pasue during fade transitions and unpause after them... Originally, I made a static bool saying if the GameManager existed, and destroyed the game object if the bool was already true to avoid duplicates. However. When reentering the original scene, the Manager wouldnt unpause my character. Can someone explain why my current code works and the previous doesn't. I think I'm fundamentally misunderstanding something because they both seem to just check if there's duplicates of the GameManager and remove it.
public static GameManager instance; void Start() { DontDestroyOnLoad(gameObject); if (instance == null) { instance = this; } else { Destroy(gameObject); }
did you have static Game$$anonymous$$anager instance in the previous code? This is a very important piece of code, this makes sure that you are accessing the exact same Game$$anonymous$$anager, no matter what scene you're at. If you dont have that, then on start, it might get confused which manager to keep, and which one to destroy.