Destroy object with dontdestroyonload

Hi,

I have some script that is attached to an object so that it isn’t destroyed as you play through level 1, level 2, level 3 and so on. However I want this object to be destroyed when the player reaches the menu screen, I’ve tried some if statements, something like

	if (SceneManager.LoadScene = "sceneSelectBeta") {
		Destroy;
	}

And tried adding some destroy functions but I can’t get the object to delete when it reaches the scene.

Here is the code attached to the object, any help or advice would be appreciated.

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class clockDontDestroy : MonoBehaviour {    

	private static clockDontDestroy _instance = null;
	public static clockDontDestroy Instance
		{
			get { return _instance; }
		}    		

void Awake()
		{
			if (_instance != null && _instance != this)
			{
				Destroy(transform.root.gameObject);
				return;
			}

		_instance = this;

		DontDestroyOnLoad(transform.root.gameObject);
	}
}

2 Answers

2

I was able to resolve this issue by creating a second script, which finds the game object and destorys it, I then attached the script to an empty gameobject on the scene where the gameobject should be destroyed. Script below:

void Start () {

	Destroy (GameObject.Find("*thegameobjecttobedestroyed*"));

}

Thank you thank you thank you so muchh!! I've been searching this for almost 3 days and can't find anything that suite mine. Love you so much.

Thank you, that's extremely helpful, I've been trying to destroy the object using FindObjectOfType, but that didn't work, while this one destroys the GameObject itself, thank you very much!!!

Can someone give me an example on the "thegameobjecttobedestroyed"))? im sorry..Im super noob in this

It doesn't work.

SO this didn't work for me however this did: On a script in the scene you want the music to stop playing when it loads do this: private void Awake() { if (SceneManager.GetActiveScene().name == "MainMenu") { Destroy(GameObject.Find("MusicPlayer")); } } Where "MainMenu" is the name of the Scene and "MusicPlayer" is the name of the Game Object that you want to destroy.

This is pretty late, but another option would be using the SceneManager.activeSceneChanged event ( Unity - Scripting API: SceneManagement.SceneManager.activeSceneChanged ).

You’d need to modify your code to something like this:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class clockDontDestroy : MonoBehaviour
{
    private static clockDontDestroy _instance = null;

    public static clockDontDestroy Instance
    {
        get { return _instance; }
    }

    public static int menuScreenBuildIndex; //the menu screen's index in your Build Settings

    void Awake()
    {
        if (_instance != null && _instance != this)
        {
            Destroy(transform.root.gameObject);
            return;
        }
        _instance = this;
        DontDestroyOnLoad(transform.root.gameObject);

        SceneManager.activeSceneChanged += DestroyOnMenuScreen;
    }

    void DestroyOnMenuScreen(Scene oldScene, Scene newScene)
    {
        if (newScene.buildIndex == menuScreenBuildIndex) //could compare Scene.name instead
        {
            Destroy(this); //change as appropriate
        }
    }
}

I think you should also remove DestroyOnMenuScreen from the event before destroying the object, just in case. I've heard that it's a good practice, from the tutorials about delegates/events