- Home /
DontDestroyOnLoad calls Awake() and Start() function each time the scene is reloaded
The function DontDestroyOnLoad doesn't seem to work for me.
void Awake () {
DontDestroyOnLoad(gameObject);
Debug.Log ("awake called");
}
void Start () {
Debug.Log ("start called");
}
Both debug messages are displayed each time i call Application.LoadLevel(0);
Is this the intended behaviour or am i doing something wrong? The Script is attached to a GameObject.
I have the same issue. I'm using Unity 5 and the GameObject is a root object.
function Awake() {
DontDestroyOnLoad(gameObject);
Debug.Log("awake called");
}
It's called every time I call :
Application.LoadLevel(0);
Answer by fafase · Mar 09, 2015 at 11:26 AM
I would think it is the intended behaviour just not the one you intended.
You mentioned that it happens every time you load the level 0. I would assume you have in your scene an object holding that script. So each time you load the scene, this object is created while the previous one is also kept. So the first time you see the Start and Awake of the first object and any time you come back, you get the Start and Awake of the new object that is created with the new loading.
Looking at your hierarchy, you would probably see multiple times the same object.
Solution:
public class MyClass:MonoBehaviour{
private static MyClass instance = null;
void Awake(){
if(instance == null){
instance = this;
DontDestroyOnLoad(gameObject);
}else if(instance != this){
Destroy(this.gameObject);
return;
}
}
}
No, a static variable is only initialised once. That's the point of it. fafase's solution does has a typo though (`instance == this` should be single '=')
Thank you very much, I was expecting too much that it behaved like I was expected to !
Answer by HankChinaski · Sep 04, 2016 at 11:18 PM
For anyone who might come across this question, a clone of your GameObject is instantiated every time you load this scene, so chances are, those calls are being made by the clone.
Don'tDestroyOnLoad() does not call Awake() or Start() when a new scene is loaded.
Answer by tormentoarmagedoom · Jul 09, 2016 at 02:39 PM
Just one thing.
Scripts only calls Start and Awake when they are loaded for first time. If you use DontDestroyOnLoad() in a script, or object with scripts, all that scripts will NOT CALL start / awake.
Answer by WILEz1975 · Sep 12, 2016 at 02:48 AM
...and if i want to call a function on the start/awake (in a new loaded scene) on a Don'tDestroyOnLoad() object?
OnLevelWasLoaded() should do the trick. Example:
using UnityEngine; using System.Collections;
public class A : $$anonymous$$onoBehaviour {
void Start(){
DontDestroyOnLoad (gameObject);
}
void OnLevelWasLoaded(){
Debug.Log ("A new scene was loaded");
}
}
Been searching for an answer to this. Thank you @HankChinaski. Won't be forgetting OnLevelWasLoaded() very soon. Cheers mate.
OnLevelWasLoaded was deprecated, you should now be using this: https://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager-sceneLoaded.html
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad 0 Answers
Don't lose variable data when reloading a level - c# 3 Answers
'DontDestroyOnLoad' is destroying other gameobjects 1 Answer
Application.LoadLevel object clone spam 0 Answers
DontDestroyOnLoad Missing Object 1 Answer