- Home /
How does "DontDestroyOnLoad" work?
I want to use it in a gameObject (this gameObject contains a AudioSource Component).I want to keep this music between scenes. Can someone explain me how to use it correctly or a example in javascript?
Thanks
But there is another problem:
This gameobject is in the "$$anonymous$$ain$$anonymous$$enu" scene (number 1) and before this scene there's another called "TitleScreen" (number 0). When I go to the TitleScreen scene from the $$anonymous$$ain$$anonymous$$enu, and return to the $$anonymous$$ain$$anonymous$$enu. The music doesn't play because it doesn't exist anymore. How can I fix this?
Sorry if I disturb too much...
Could someone answer this guy's question? I would like to know the answer too.
Answer by oddgoo · Jul 14, 2014 at 02:46 AM
It's very simple, and it can indeed be used to keep the music consistent between the scenes.
The Unity Docs give the following example:
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
Which means that, as long as you add DontDestroyOnLoad(transform.gameObject); to the Start or Awake function of a script attached to the gameobject with the AudioSource, that object will remain in-between scene changes.
In other words, say you add the gameObject to a "MainMenu" scene, then you attach the script with DontDestroyOnLoad to it. Once you move from "MainMenu" to a "Gameplay" scene, the gameobject will remain in the hierarchy.
BUT, if you start from the "Gameplay" scene straight away, the gameobject will not be there, since it was "authored" on the "MainMenu" scene. It doesn't create itself automatically, it only prevents itself from being deleted on scene changes like every other gameObject.
Thanks ;) but, how can I destroy this gameObject when it already exists in the scene? I mean: I am in the "$$anonymous$$ain$$anonymous$$enu" scene then I go to another scene, and from this scene I go back to the "$$anonymous$$ain$$anonymous$$enu" scene. There are two gameobjects playing the same music.
Thanks again :D
I made this script to my music gameobject: private static var created = false;
function Awake ()
{
if (!created)
{
DontDestroyOnLoad(this.gameObject);
created = true;
}
else
{
Destroy(this.gameObject);
}
}
function OnLevelWasLoaded (level : int)
{
if(level==0)
{
Destroy(this.gameObject);
}
}
But there is another problem:
This gameobject is in the "$$anonymous$$ain$$anonymous$$enu" scene (number 1) and before this scene there's another called "TitleScreen" (number 0). When I go to the TitleScreen scene from the $$anonymous$$ain$$anonymous$$enu, and return to the $$anonymous$$ain$$anonymous$$enu. The music doesn't play because it doesn't exist anymore. How can I fix this?
Sorry if I disturb too much...
I know this is almost a year later, but I'd like to help just incase you're still having this problem, or if anyone else has this problem and comes to this thread. I'm not that great at scripting, but I think I might have a solution. Ins$$anonymous$$d of destroying the gameObject on level change to the main menu, just turn off the audio source component, and turn it back on if it's not the main menu. Here's the docs page for more: http://docs.unity3d.com/ScriptReference/AudioSource-mute.html
The script would look something like this:
function Awake ()
{
DontDestroyOnLoad(this.gameObject)
if(onmenu==true)
{
audio.mute = true;
}
if(onmenu==false)
{
audio.mute = false;
}
}
I haven't tested it, but give it a try and let me know what happens.
Answer by Irongaze · Oct 03, 2015 at 08:14 PM
Another (probably obvious) issue is that if you have your GameObject as a child of another object, call DontDestroyOnLoad on the child, it will still get destroyed. Seems like the entire tree needs to have DontDestroyOnLoad called on the root GameObject.
Thanks , that should be mentioning in the first place because it's not obviously .
Answer by YawJatah · Feb 01, 2018 at 04:12 AM
Hi Guys, I was also wrangling with the whole DontDestroyOnLoad, and multiple instance until i feel on the following tutorial: https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager.
Basically Answers by @Yoshi979797, almost works, the problem is when you load a scene directly. The solve this, you have to implement the Singleton pattern, but in a unity way. The proper solution will look something like this:
public class NameOfYourClass : MonoBehaviour
{
public static NameOfYourClass instance = null;
//Awake is always called before any Start functions
void Awake()
{
//Check if instance already exists
if (instance == null)
{
//if not, set instance to this
instance = this;
}
//If instance already exists and it's not this:
else if (instance != this)
{
Destroy(gameObject);
}
//Sets this to not be destroyed when reloading scene
DontDestroyOnLoad(gameObject);
}
}
It is extremely important to have the Static instance, this allows you to carry variable states over from the previous scene (if you are coming from a previous scene) or properly create a new instance if you load directly into a scene. Took me a while to figure this out, so wanted to give back to the community (which as been amazing and super helpful)
Hello, I want the gameobject from the previous scene to be destoy, how to do that ?
Answer by Yoshi979797 · Jul 22, 2017 at 07:10 PM
Here is a solution that works in 5.6.2f1
static bool created = false;
void Awake() {
if (!created) {
DontDestroyOnLoad (this.gameObject);
created = true;
} else {
Destroy (this.gameObject);
}
}
}
Awesome!
if (!created) {
DontDestroyOnLoad (this.gameObject);
created = true; // which runs "else" statement exactly the next frame )))
} else {
Destroy (this.gameObject);
}
he put it in awake() which only runs once and at the start of game.
This isn't strictly true, I believe. I found out the hard way that Awake() will be called upon a scene change, too.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to pickup and equip an object? 0 Answers
What is a good unityscript writing program? 1 Answer
How to make a selection system? 1 Answer
How to spawn objects in a specific range of random location 1 Answer