DontDestroyOnLoad(gameObject); not working, what am I doing wrong?

I’m currently watching this Unity Tutorial on the UI and how to have background music come along in multiple scenes. The problem is that the “DontDestroy” script somehow doesn’t work?

Around (24:30)

This is the code I’m using. Google has some slight variations, but most come back to this simple one.

using UnityEngine;
using System.Collections;

public class DontDestroy : MonoBehaviour {

    void awake()
    {
        DontDestroyOnLoad(gameObject);
    }
}

I attach the script to my backgroundmusic object, I press play and then press the button that loads the next scene, but for some reason this object is still destroyed and the music stops… What am I missing here?

Gotta Capitalize That Awake Method, Mate.

1 Like

Yeah bud, make sure that “void awake()” becomes “void Awake()”

1 Like

It’s “Awake”, not “awake”.

1 Like

I was just in the documentation and copy & pasted this:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Awake() {
        DontDestroyOnLoad(transform.gameObject);
    }
}

Now my soundtrack goes on to the next scene.

That’s an hour wasted, but at least I learned to check for capital letters. Thanks for helping mates :slight_smile: