- Home /
Destroy dontDestroyOnLoad how do I do it?
So what I need to do is keep the score text in levels 1-4 but in the congratulations & game over levels I don't need this information. Basically I need the text & canvas to delete themselves when I need them to delete themselves because even 3-4 scenes after I no longer need the scoreText it's still there because of the dontDestroyOnLoad. Here is my jumbled up code which may be me heading in the wrong direction. using UnityEngine; using UnityEngine.SceneManagement;
public class ExampleCode : MonoBehaviour { // called zero void Awake() { Debug.Log("Awake"); }
// called first
void OnEnable()
{
Debug.Log("OnEnable called");
SceneManager.sceneLoaded += OnSceneLoaded;
}
// called second
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("OnSceneLoaded: " + scene.name);
Debug.Log(mode);
}
// called third
void Start()
{
Debug.Log("Start");
}
// called when the game is terminated
void OnDisable()
{
Debug.Log("OnDisable");
SceneManager.sceneLoaded -= OnSceneLoaded;
}
} /Look up the sceneChanged delegate in the API and read the example. Then apply the example to your own project. You know if-statements, so you just have to figure out which scene was loaded. Check the SceneManager class in the API. You’ll find something that will solve this problem for you. What I understood is that you want to destroy something in a specific scene. This means that you need to check something when a scene got loaded. And that something is: Which scene got loaded? Before you implement any complex logic, you just log the name of the current scene into your console after that scene got loaded. And when you load another scene, you do the same again. And where do you do that? * In a script which is attached to one of your persistent game objects (that are supposed to get destroyed in a certain scene).
Step by step. Just keep it simple. Use the example from the API. If in doubt, copy and paste it as it is. Then log the name of the current scene into your console. And then share either the result or what you managed to do before you got stuck.
Once this works, the rest is not that difficult anymore.
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html
Below is what I'm trying to do. Above is Nina's replies, the script is the one on the link. “don’tDestroyOnLoad” to keep the score & other elements transferring from scene to scene. Is there a way to destroy that on a certain scene? What I mean is I need dontDestroy to work while I’m on stages 1-4. But on the congratulations, main menu & game over stages I’d like to destroy these gameObjects that are no longer necessary.
I tried a basic destroy code that didn't work here is an example
void OnEnable() { SceneManager.sceneLoaded += OnSceneLoaded; }
void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// here you can use scene.buildIndex or scene.name to check which scene was loaded
if (scene.name == "MainMenu")
{
// Destroy the gameobject this script is attached to
Destroy(gameObject);
}
------- & even more basic
void Start() { Destroy(GameObject.Find("object to be destroyed"));
}
If you are wishing to destroy an object you have already established as a singleton with a don’t destroy on load ability, you might need to re-think how you are setting up that object. The whole point of Don’t Destroy is so you won’t destroy the object. If you are wishing to destroy that object only on certain scenes, but use it on other scenes, then may I suggest a looking into making it a child of a singleton and using .setActive(true/false) https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html
*/
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad() does not seem to be working. 3 Answers
FindGameObjectsWithTag not finding a tagged object 4 Answers
DontDestroyOnLoad duplication. 0 Answers
Next LEVEL Load Problem. 2 Answers
DontDestroyOnLoad alternative? 2 Answers