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

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.

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.

Hi Guys,
I was also wrangling with the whole DontDestroyOnLoad, and multiple instance until i feel on the following tutorial:
Level Generation - Unity Learn.

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)

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);
	}
}

}