Hi, I’m having a hard time with this issue, currently I have Timer as the top layer inside it is the text and the image for my timer. i use dontDestroyOnLoad for my timer to pass through some scenes that needed a timer, but when I loaded the main menu the timer is still there. how can i destroy a GameObject that has dontDestroyOnLoad attached to it?
Add this to the script in the Timer gameobject:
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);
}
}