Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by windsands · Nov 22, 2021 at 04:49 PM · dontdestroyonload

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

*/

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

130 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

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


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