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