[Help] Play the game in Unity5 has a Error : NullReferenceException

Hello,
i’ve been doing to upgrade my game which is created in Unity4 to 5
and i play the game in Unity5
other things are going well , but when i click the “Play Again” button in the “Win Screen”

it saids

NullReferenceException: Object reference not set to an instance of an object
MusicPlayer.OnLevelWasLoaded (Int32 level) (at Assets/Scripts/MusicPlayer.cs:29)

this screenshot is part of the MusicPlayer.cs

(i know level 0 and 1 is the same startClip, i just want to play the same music file )

i don’t know why this Error is keep coming
this kind of error is not exist in Unity4

i think no one get this kind of error but me :frowning:

Thank you for reading

Hello, how are you?
The problem seems to be on the music.Stop(), would you mind sending the whole code?
You could try to add an if statement to make sure there is an music adressed to it:

if(music) {music.Stop();}
else{music = whatever music should be;}

If this dont solve the problem, could you send the entire code?

Hi I got the same error.

this is the whole code.

public class MusicPlayer : MonoBehaviour {
static MusicPlayer instance = null;

public AudioClip startClip;
public AudioClip gameClip;
public AudioClip endClip;

private AudioSource music;

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

void OnLevelWasLoaded(int level){
	music.Stop();
	if(level == 0){
		music.clip = startClip;
	}
	if(level == 1){
		music.clip = gameClip;
	}
	if(level == 3){
		music.clip = endClip;
	}
	music.loop = true;
	music.Play();
}

}

Please help me solve it?

Do you have exactly the same error, or just a null reference exception generally? A screenshot with tye error wpuld be useful.

Hi the error is:

NullReferenceException: Object reference not set to an instance of an object
MusicPlayer.OnLevelWasLoaded (Int32 level) (at Assets/Scripts/MusicPlayer.cs:29)

and when i double click it it opens MusicPlayer.cs en goes to the line 29 where the music.Stop() method is called

Hi,

Ok, so wjat it is tellinf you then is that music is null, e.g. doesn’t exist, yet you are trying to call one of it’s methods.

In @Joao_Dalvi’s post above you can see that he suggests placing a check around music to see that it actually exists first, try giving that a go.


Example;

if(music) 
{
    music.Stop();

    if(level == 0)
        {
		music.clip = startClip;
	}
    
    if(level == 1)
        {
		music.clip = gameClip;
	}
    
    if(level == 3)
        {
		music.clip = endClip;
	}

    music.loop = true;
    music.Play();
}
2 Likes

Thank you!
I didn’t read it correctly thank you for your help.
My apologies for the duplicate question.

1 Like

It’s no trouble at all. :slight_smile:

Incidentally, if you move the code from the Start() method to an Awake() method (example below), you don’t need the check at all.


Example;

    /// <summary>
    /// Pre-Initialisation
    /// </summary>
    private void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            instance = this;
            GameObject.DontDestroyOnLoad(gameObject);
            music = GetComponent<AudioSource>();
            music.clip = startClip;
            music.loop = true;
            music.Play();
        }
    }

See also;

2 Likes

Privacy & Terms