Pausing Music

Hey, Guys! I’m making a space exploration game. In my “Main Menu” scene, I have a GameObject that has an Audio Source (my music), and a script that has a DoNotDestroyOnLoad function in it.

Here is the script:

using UnityEngine;
using System.Collections;
public class DontDestroy : MonoBehaviour {
	void Awake ()
	{
		GameObject[] objs = GameObject.FindGameObjectsWithTag("music");
		if (objs.Length > 1)
			Destroy(this.gameObject);
		DontDestroyOnLoad(this.gameObject);
	}
}

I could not for the LIFE of me, figure out how to pause and unpause this music via my pause menu, which was in a completely different scene, called “Space.” Is there anything I could put into the following script to fix this.

using System.Collections;
using UnityEngine;
public class PauseGame : MonoBehaviour {
	public Transform canvas;
	public GameObject player;
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown (KeyCode.Escape)) 
		{
			if(canvas.gameObject.activeInHierarchy ==false)
			{
				player.GetComponent<PlayerMovement> ().enabled = false;
				canvas.gameObject.SetActive (true);
			
			} else 
			{
				player.GetComponent<PlayerMovement> ().enabled = true;
				canvas.gameObject.SetActive (false);
			
			}	
		}
	}
}

Thank You for you help anyone .

Since your music object probably is one-of-a-kind, a static variable referencing the object should work fine.

Short info: “static” for a variable means that the variable is not bound to an object. Regular variables have one value for each instance (aka. one light is red, another might be blue). Static variable values don’t exist once per object, but once per program. This has the side effects that the variable does not die with an object (this allows to keep values through scene changes without DontDestroyOnLoad) and that you can access them without specifying an object.

The latter is the interesting part for you in this case. Your problem is that you don’t know how to get a reference to your object since you cannot set it in the editor, because the two objects don’t exist in the same scene. A static variable’s value can be checked at any time from any object without knowing anything, so you can retrieve that reference.

In action, it looks like this:

public static DontDestroy singleton { private set; get; }
public AudioSource source { private set; get; }

void Awake()
{
  if(singleton)
  {
    Destroy(gameObject);
  }
  else
  {
    singleton = this;
  }
}

Note that this code replaces your current code that checks if the this object already exists.
Also note that this script should have a different name, because “DontDestroy” does not hint that there is some special semantic to the object that would make it one-of-a-kind. I’ll stick with the name for now though.
By the way, this syntax

{ private set; get; }

makes the variable read-only to other classes.

In the other script, you can simply ask the class (!) for the static variable’s value:

DontDestroy.singleton

Use it like this:

DontDestroy.singleton.source.Pause();