Someone help me please! I am really tired
Hello hero, so I am beginner, and basically what i want to do is, Whenever I switch scenes while playing, I have this button Esc. as pauses the game and loads u to a screen where it has paused.... the problem is I can't stop the music already playing at the beginning of the start, where it keep on playing and when i press Esc. i get to the paused scene, but the old music is still playing with the other music which is really annoying, Here is the code below.
The errors i get are 2, first is error CS0019: Operator !=' cannot be applied to operands of type MusicPlayer' and string' second is error CS0428: Cannot convert method group DestroyObject' to non-delegate type `MusicPlayer'. Consider using parentheses to invoke the method
using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour
{
static MusicPlayer instance = null;
void Awake ()
{
Debug.Log("Music player Awake" + GetInstanceID());
if (instance != null)
{
Destroy (gameObject);
Debug.Log("Duplicate Music player self-destructing!");
}
else if (instance != ("Level_01") || instance != ("Level_02"))
{
instance = DestroyObject;
}
else
{
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
}
You need to learn how to use the Format Code feature of UA to make your code readable by other users (this is the “101/010” button above). For more information see the user guide here. I have done it this time for you.
Answer by Landern · Aug 29, 2016 at 02:16 PM
Well, this part:
else if (instance != ("Level_01") || instance != ("Level_02"))
{
instance = DestroyObject;
}
makes no sense, instance is not a string so comparing is a no go, it would appear you have no operator overrides to take in a string thats wrapped in parenthesis for some reason, did you want to compare the active scene? Anyways, that else/if make no sense whatsoever(including the DestoryObject to instance assignment.
Your answer