Preventing my game object from multiplying due to DontDestroyOnLoad()

I’ve been trying to implement a game object that is created at the “mainmenu” scene of my game that is intended to be omnipresent. This would allow me to easily manage things such as save data, user inventory, and state management all within one or multiple classes.

Currently, I have the Game_Manager class extending from MonoBehavior which runs the DontDestroyOnLoad() function inside it’s Start() function. This works well and the Game_Manager game object exists even upon scene transitions (currently to a test scene), yet the current set up allows the game object to multiply itself as it is re-created upon loading the original main menu scene. In other words, whenever I switch back to the main menu level it will create another Game_Manager object.

One solution for this would be to make a “Initialize” scene which only has the function of introducing omnipresent game objects and will never be loaded again during runtime, but this solution seems less than ideal. Is there any way to prevent the game object from creating itself if one of a similar name already exists using a simple unity function / parameter or should I instead rely on creating a singleton gameobject? Feel free to throw in any other suggestions. I’m using C# for all scripting / programming in unity thus far.

Use a static variable to work out whether the object has already been loaded - destroy any copies if the static is set.

 public class MyPersistentThing : MonoBehaviour
{
      public static MyPersistentThing Instance;

      void Awake()
      {
            If(Instance)
                DestroyImmediate(gameObject);
            else
            {
                DontDestroyOnLoad(gameObject);
                Instance = this;
            }
      }
}

My solution, which is more straightforward for numskulls like me, was to attach this script to any persistent GameObject. Seems to work. . . so far. This script should work unmodified with any GameObject that has a tag.

public class DestroyDuplicateOnLoad : MonoBehaviour
{
    private void Awake()
    {
        var otherPersistent = GameObject.FindWithTag(gameObject.tag);
        if (otherPersistent != null && otherPersistent != transform.gameObject)
            DestroyImmediate(transform.gameObject);
    }
}