Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by gaia2222 · Dec 22, 2020 at 07:13 PM · scene-switchingdontdestroyonloadnewinitialization

How does DontDestroyOnLoad working?

Every time when the scene has been load(in the same scene), some code in awake is running and something is not.

 public class ScenesManager : MonoBehaviour
 {
     public List<int> test;
 
     private void Awake()
     {
         DontDestroyOnLoad(gameObject);
         test = new List<int>();//this code have not run
         Debug.Log("test is the new List now");//this code is running
     }
 
 }

Comment
Add comment · Show 5
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 highpockets · Dec 22, 2020 at 08:04 PM 0
Share

Why do you say the generic list is never created?? Did you try to see if the test has a Count of 0: Debug.Log(test.Count);


DontDestroyOnLoad() will make sure the game object is not destroyed when you load a new scene. It will persist

avatar image Llama_w_2Ls · Dec 22, 2020 at 08:04 PM 0
Share

How do you know this line of code isn't working? test = new List<int>();//this code have not run $$anonymous$$aybe it has but another script's Start() method changed it again. Awake() is ran before Start(). @gaia2222

avatar image gaia2222 · Dec 22, 2020 at 08:39 PM 0
Share

Yes, Also I try some different way to check the test List, I try to add and change the value. After reload the scene, the value have not any change and keep the old value. It's show that {test = new List();} have not run. But the console can also log the message.

avatar image highpockets gaia2222 · Dec 22, 2020 at 09:06 PM 0
Share

What is your desired behavior? I don’t get what you are trying to do. That line of code is surely running, but I think we are missing some info here. If you give some more details, it would likely help to find out what your problem is.

avatar image gaia2222 highpockets · Dec 23, 2020 at 07:19 AM 0
Share

Oh sorry, I miss the important point. That situation only happen in using "loadsceneasync" to reload the scene.... I am sorry for missing the important point

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by highpockets · Dec 23, 2020 at 08:13 AM

So do you have this SceneManager class setup as a singleton?? Because if it is not a singleton, you are creating a new instance of this class every time you reload the scene. If this is the case, you will have to rethink what you are doing. Consider the following: (a very simple singleton implementation)


 public class ScenesManager : MonoBehaviour
  {
      public List<int> test;
  
      private void Awake()
      {
          ScenesManager _instances = FindObejctsOfType<ScenesManager>();
          
          if( _instances.Length > 1 )
          {
               for( int i = 0; i < _instances.Length; i++)
               {
                    if( _instances[i] != null && _instances[i] != this )
                    {
                        Destroy( this.gameObject );
                        return;
                    }
               }
          }
          DontDestroyOnLoad(gameObject);
          test = new List<int>();//this code have not run
          Debug.Log("test is the new List now");//this code is running
      }
  
  }
 




Comment
Add comment · 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
0

Answer by CmdrZin · Dec 23, 2020 at 08:03 AM

What ever GameObject that this script is attached to does not go away when the scene it is in is replaced by another scene. The GameObject is persistent form scene to scene. See also https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Comment
Add comment · 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

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

113 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Canvas will only appear on the game screen when its child of a new loaded scene canvas 1 Answer

Question Regarding Don'tDestroyOnLoad Object 1 Answer

Can't call script from another scene using DONTDESTROYONLOAD(). 3 Answers

When die - reset score from the last scene 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