0
\$\begingroup\$

I have a GameObject in my main menu and help scenes that I want to carry across both scenes but be destroyed on all other scenes. I am having multiple issues with it such as it wont destroy on other scenes or when I go back to the previous scene it will generate 2 GameObjects and so on. Here is the script:

public class DestroyObject : MonoBehaviour
{
    void Awake()
    {
        DontDestroyOnLoad(gameObject);
        SceneManager.sceneLoaded += OnSceneLoaded;
    }

    void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode)
    {
        if ((scene.name == "Help") || (scene.name == "Main Menu"))
        {
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
}
\$\endgroup\$
5
  • 2
    \$\begingroup\$ you probably want to instantiate only one instance and then disable/enable it when you do or don't need it \$\endgroup\$ Commented Aug 11, 2019 at 23:13
  • \$\begingroup\$ @BigTLarrity How do i do that? \$\endgroup\$
    – Ethan K-B
    Commented Aug 12, 2019 at 8:43
  • 1
    \$\begingroup\$ Have your considered loading your help scene additively, so it doesn't unload your main menu, just hides it? Then your music can stay in just the main menu scene. \$\endgroup\$
    – DMGregory
    Commented Aug 12, 2019 at 13:28
  • 1
    \$\begingroup\$ DM Gregory's answer is almost certainly the right way to go, but for a simple approach you could add a script to the object you want to stay alive that uses DontDestroyOnLoad. Then use gameObject.SetActive(true/false) when you want to enable/disable it. One problem with this way, is that when the gameobject is not active the script wont run to make it active again. So making it Active/Inactive will need to be done from another gameobject such as a Game_Manager class for example \$\endgroup\$ Commented Aug 12, 2019 at 19:22
  • \$\begingroup\$ @BigTLarrity I see what your saying. Can you give me a small bit of sample code to start with because I normally need to see something first to be able to write the code. \$\endgroup\$
    – Ethan K-B
    Commented Aug 13, 2019 at 4:37

1 Answer 1

1
\$\begingroup\$

You could have a gameobject in the scene called GAME_MANAGER. Add to it a script 'Game_Manager.cs'

have a public method inside of this script:

public static Game_Manager instance; //create a singleton instance to access these methods from other scripts.
private GameObject menuObject;  // store reference to the menuobject you want to activate

void Awake(){
// init the singleton instance if it is not already
if (instance == null)
     instance = this;
else
     Destroy(this);

DontDestroyOnLoad(gameObject); // make sure this object isn't destroyed after scene changes

menuObject = GameObject.Find("Name Of The Object"); // this is one way of many to 'find' the object you are wanting.
}

public void ActivateMenuObject(bool makeActive){
     menuObject.SetActive(makeActive);
}

You could call this 'ActivateMenuObject' from any script by accessing the singleton instance:

Game_Manager.instance.ActiveMenuObject(true);

or you could find another way , for example do it within the Game_Manager itself, or you could add another parameter to the ActiveMenuObject method asking for a GameObject, and then the method would SetActive the specified GameObject to the specified bool without needing to make a reference to the gameobject in the Game_Manager.

    public void SetObjectActiveState(GameObject obj, bool active){
     obj.SetActive(active);
}

I do hope that helps. I'm not an expert but I have been tinkering with Unity at noob level for a few years :) Good luck!

\$\endgroup\$
2
  • \$\begingroup\$ I am sure I am not understanding it properly, but it looks like it wont destroy the GAME_MANAER object, but it will still destroy the audio source I want to carry over. \$\endgroup\$
    – Ethan K-B
    Commented Aug 13, 2019 at 19:01
  • \$\begingroup\$ I must admit I don't use SceneManagement much in any of my projects. I manage the scenes myself all within one scene most of the time (as I say I am still learning myself). But I thought the components attached to gameobjects with a DontDestroyOnLoad() call, would not be destroyed when scenes are loaded. I might be completely wrong and I am sorry if I've ended up giving you incorrect info docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html \$\endgroup\$ Commented Aug 13, 2019 at 23:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .