Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

New game isn't reloading gameobjects that had a Don'tDestroyOnLoad method and then were destroyed.

Discussion in 'Scripting' started by hanann24, Sep 3, 2017.

  1. hanann24

    hanann24

    Joined:
    Aug 3, 2017
    Posts:
    1
    My game has a gameobject that has a DontDestroyOnLoad method. I also have a pause menu that can access the main menu which is in a difference scene. My problem is that if I access the main menu from the pause menu then the gameobject carries over into the main menu scene. Yet if I destroy the gameobject, when clicking the new game button which loads the main scene then the gameobject is still destroyed.
    How would I go about resetting my main scene when I access it from the main menu so then the gameobject is created again?
    Thank you.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Tip one: Don't destroy a GameObject that you don't want destroyed. ;)

    I tend to have a lazy service instantiation script in every scene. It checks for the presence of the services scene, and then loads it if its not present. It makes it really convenient for play testing, as I can start the game from any point and the appropriate services will be loaded.
     
    Ryiah likes this.