DontDestroyOnLoad not working?

Hi,

I have an object in my main menu scene with an audio source component + audio file, and a script attached called scriptBackgroundMusic:

#pragma strict

private static var instance:scriptBackgroundMusic;
public static function GetInstance() : scriptBackgroundMusic {
	return instance;
}

function Awake() {
    if (instance != null  instance != this) {
        Destroy(this.gameObject);
        return;
    } else {
        instance = this;
    }
    DontDestroyOnLoad(this.gameObject);
}

…the music plays on the main menu. However, when I start level 1 the music stops. I can’t figure out why this is happening. Any ideas?

My level load script is simply:

function OnGUI(){
	
	if(GUI.Button(Rect(375,250,100,30), "Start Game"))
	{ 	
		Application.LoadLevel("sceneLevel01");		//Load the game level
	}
	
	if(GUI.Button(Rect(375,290,100,30), "Exit"))
	{
		Application.Quit();							//Exit game (only works in final build)
	}
}

Thanks!

I just copy and pasted your scripts to my project and they’re working fine. The music keeps playing after loading the new scene via the gui button. It must be something else you have going on.

Thanks for doing that, much appreciated! I will go through the rest of my code line by line and see if something else is causing it not to run.

Cheers!