Using singleton for background music

I'm trying to play background music through multiple scenes by creating a Game Object in my main menu and using the code below:

  public class MusicPlayer : MonoBehaviour {
  private static MusicPlayer instance = null;
  public static MusicPlayer Instance { get { return instance; } }

  void Awake() {
    if (instance != null && instance != this) 
        {
        Destroy(instance.gameObject);
        return;
    } else {
        instance = this;
    }

    DontDestroyOnLoad(gameObject);
  }

  public void Play() {
    audio.Play();
  }

  public void Stop() {
    audio.Stop();
  }
}

In my Menu script...

void Start () {
        MusicPlayer.Instance.Play();
    }

The music keeps playing when I go to other scenes, but it stops once I return to the main menu.

If you use DontDestroyOnLoad(gameMusic) it will "survive" a scene load. In you first script you are destroying the gameMusic object manually every time you load a new scene. If you don't do that it should work.

To use the Singleton pattern just replace the class name with your own. Then add you own code to it. You can now reach this object from every other script using MyClassName.Instance.

Here is the quick and dirty version for you:

[RequireComponent(typeof(AudioSource))]
class MusicPlayer : MonoBehaviour {
  private static MusicPlayer instance = null;
  public static MusicPlayer Instance { get { return instance; } }

  void Awake() {
    instance = this;
    DontDestroyOnLoad(gameObject);
  }

  public void Play() {
    audio.Play();
  }

  public void Stop() {
    audio.Stop();
  }
}

If you attach this script to your gameMusic object the object shouldn't be destroyed and you can start stop your music by calling MusicPlayer.Instance.Play() from any script.

This is what I finally cobbled together that worked for me.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioListener))]
[RequireComponent(typeof(AudioSource))]
class Music : MonoBehaviour
{
	private static Music instance = null;
	public static Music Instance
	{
		get
		{
			if (instance == null)
			{
				instance = (Music)FindObjectOfType(typeof(Music));
			}
			return instance;
		}
	}
	
	void Awake ()
	{
		if (Instance != this)
		{
			Destroy(gameObject);
		}
		else
		{
			DontDestroyOnLoad(gameObject);
		}
	}
}

Messing around with the code a bit and testing with my game I have found the code below to work. The music plays throughout all scenes and doesn't start and stop when going in and out of the menu. I also just Play the music within the class for when the instance first gets set to this. Don't know if this is sloppy c# coding, but still just getting my feet wet with this language.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))]
class MusicPlayer : MonoBehaviour {
  private static MusicPlayer instance = null;
  public static MusicPlayer Instance { get { return instance; } }

  void Awake() {
    Debug.Log("instance = " + this);
    if (instance != null){
        Debug.Log("inside the instance != null if statement doing nothing");
        return;
    } 
    else {
        instance = this;
        DontDestroyOnLoad(gameObject);
        Play();
    }
  }

  public void Play() {
    audio.Play();
  }

  public void Stop() {
    audio.Stop();
  }
}