- Home /
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
}
}
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
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
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.
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.
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
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
}
}
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
Your answer
Follow this Question
Related Questions
Big problem with a small singleton 1 Answer
How do you transfer UI Text from one scene to another? 1 Answer
Don't destroy On Load make object not interactable in new scene 0 Answers
Carry level slection menu through all levels 2 Answers
Best way to deal with DontDestroyOnLoad when returning back to the same scene? 1 Answer