Getting music to play

Hey,

I want to play some music in my game but I can’t get it to play, I already attached the script to a game object that I placed inside the scene. this is my code:

static MusicPlayer instance = null;

 void Awake()
 {
     if (instance != null)
     {
         Destroy(gameObject);
     }
     else
     {
         instance = this;
         GetComponent<AudioSource>().Play();
         DontDestroyOnLoad(gameObject);
     }
 }

Thanks in advance.

You need to attach an audiosource next to your script

If this is not your issue here, maybe try

AudioSource audioSourceMusic;
 void Start()
    {
        audioSourceMusic = gameObject.AddComponent<AudioSource>();
    }

void PlayMusic(AudioClip clip)
{
    audioSourceMusic.clip = clip;
    audioSourceMusic.Play();
}

or at last