Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Aug 14, 2016 at 06:28 PM by ethranes for the following reason:

The question is answered, right answer was accepted

avatar image
3
Question by ethranes · Aug 14, 2016 at 05:42 PM · c#destroyscene-switchingdontdestroyonloaddestroy object

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


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
13
Best Answer

Answer by ethranes · Aug 14, 2016 at 06:28 PM

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*"));
 
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fajriardiansyahh · Dec 14, 2018 at 04:17 AM 1
Share

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

avatar image Georges_d · Jan 06, 2019 at 08:31 PM 1
Share

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!!!

avatar image nathi0106 · May 07, 2019 at 07:12 AM 0
Share

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

avatar image damobe · Nov 21, 2020 at 05:04 PM 0
Share

It doesn't work.

avatar image NeilLondon · Feb 27 at 01:51 PM 0
Share

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.

avatar image NeilLondon NeilLondon · Feb 27 at 01:54 PM 0
Share

Of Course now the original way ethranes posted is working >.<

avatar image
1

Answer by cablay · Apr 17, 2017 at 08:59 AM

This is pretty late, but another option would be using the SceneManager.activeSceneChanged event ( https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-activeSceneChanged.html ).

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
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MyChade · Feb 26, 2018 at 11:05 AM 0
Share

I think you should also remove DestroyOn$$anonymous$$enuScreen 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

Follow this Question

Answers Answers and Comments

225 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy() not removing the object all the time (Coroutine used) 0 Answers

How to destroy object after it moves out of screen 7 Answers

SetActive is destroying objects instead of settings them as false 0 Answers

How to keep selected player active and all others inactive when new scene loaded 3 Answers

Player falls through destroy collider 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges