MusicPlayer is being destroyed when clicking "Play Again", game crashes

I am almost done with this section but i can´t find the answer to this problem.
In the “Win screen” when i try to Play Again, the game crushes and the error says: “NullReferenceException: Object reference not set to an instance of an object
MusicPlayer.OnLevelWasLoaded (Int32 level) (at Assets/Scripts/MusicPlayer.cs:31)”

So the line is this one:

void OnLevelWasLoaded (int level)
{	
	music.Stop();    <-------------------------------- This one
	if(level == 0){
		music.clip = startClip;
	}
	if(level == 1){
		music.clip = gameClip;
	}
	if(level == 2){
		music.clip = endClip;
	}
	music.loop = true;
	music.Play();
}

I debuged the code and when the game is re starting the music player is being destroyed. This happens here:

void Start () {
	if (instance != null && instance != this) {
		Debug.Log(instance.ToString());
		Destroy (gameObject); <----------------------------------------**HERE**
		//print ("Duplicate music player self-destructing!");
	} else {
		instance = this;
		GameObject.DontDestroyOnLoad(gameObject);
		music = GetComponent<AudioSource>();
		music.clip = startClip;
		music.loop = true;
		music.Play();
	}

So my main question is: Why is this happening?. I am setting the “instance” variable to “this” when the game starts so
i cannot get my head around why is this happening.

Sorry if this is a very noob question but it would really help any response.

Thanks

instance is a static GameObject? I don’t like to use just “this”, try changing it to this.gameObject in both the if and in the else, this usually refers to the script and not to the GameObject itself.

That’s actually a common way to set up a Singleton in Unity.

I remember dealing with this on a previous project.

(deleted incorrect answer)

1 Like

Hmm I’ve just checked the course code and it’s exactly as you have it. Maybe I’m mistaken. Are there any differences between your code and this? https://github.com/CompleteUnityDeveloper/06-Laser-Defender/blob/master/Assets/Scripts/MusicPlayer.cs

edit see here as well Music Player in Unity 5

Hello @ninjachimp. The code i have in mu script Is exactly the same that is why i am so confused. I tried what you suggested, even removing the else statement, but the error is still there. Seriously No clue what is going on.

Thanks for the previous answer

I think there’s a bug in the course code as a few others have seen it. Check here Music Player in Unity 5

A solution is given in the next section, Glitch Garden: create the music manager on an init/splash screen scene that automatically loads the first screen. Then, never load the splash scene again.

Aye this is what I do now. All my managers get set up on the splash.

@ninjachimp Thanks a lot for pointing me in the right answer. I am glad i had an idea of what the error was, but i couldn´t find the answer on my own.

I used the Awake method and followed all the steps of the answer given in the other Chat.

Cant fully understand why the awake method works and not the Start method. if someone can clarify this a little bit i would really appreciate it.

@Todd_Vance @ninjachimp

Thanks in advance.

It’s to do with execution order. https://docs.unity3d.com/Manual/ExecutionOrder.html

Essentially, your OnLevelWasLoaded() method was being called before your Start() method (as is always the case) but at that time, the second MusicPlayer hadn’t yet destroyed itself or initialised the music field. So the OnLevelWasLoaded method tried to call .Stop() on a null, which gives you that exception.

By moving the singleton code to the Awake() method, which is guaranteed to happen before the OnLevelWasLoaded() method, the singletons have been properly set up and the new one destroyed before it has a chance to make a null reference to music.

Awesome answer @ninjachimp i really appreciate it. Thanks a lot

1 Like

So I’ve been going through this a couple of times as I had the same error but the solutions posted didn’t seem to work for me, please let me know if I missed anything, but isn’t the solution as simple as changing this in the MusicPlayer script:

private AudioSource music;

to this:

private static AudioSource music;

Cause that seems to work for me. I might be totally off with that, but the error is gone and it would make sense to make this AudioSource static, as all we are changing is the Clip, not the AudioSource. I’d appreciate any feedback, as I’m not sure if I’m right about this at all, all I know is that the error is gone now :wink:

4 Likes

I think you’re right. Your solution is the only one that works for me.

Privacy & Terms