# How do I make a GameObject carry across only 2 specific scenes?

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()
{
}

{
if ((scene.name == "Help") || (scene.name == "Main Menu"))
{
}
else
{
Destroy(gameObject);
}
}
}

• you probably want to instantiate only one instance and then disable/enable it when you do or don't need it – Big T Larrity Aug 11 at 23:13
• @BigTLarrity How do i do that? – Ethan K-B Aug 12 at 8:43
• 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 – Big T Larrity Aug 12 at 19:22
• @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. – Ethan K-B Aug 13 at 4:37

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.
}

}


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!

• 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. – Ethan K-B Aug 13 at 19:01
• 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 – Big T Larrity Aug 13 at 23:28