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);
}
}
}