Unsure on how to access a var from another function

Hello, I have this code here that creates and destroys a main music system. The reason I want it to destroy the music is because in my level when I finish the level I want a special jingle to play but when the menu appears again (as of right now there is only one level) the music keeps playing. But now I get an error that says that the code is unsure of the variable I instantiated. How can I access the variable in another function?

This is the code of the stop music function.

 public static function StopAllAudio() {
    	mManager.Destroy(AudioSource);
    	for(var audioS : AudioSource in allAudioSources) {
        	audioS.Stop();
        }
    } 

and this is the code of the create music function.

function Start () 
{
	currentScore = 0;
	
	if (!GameObject.FindGameObjectWithTag("MM")) {
		var mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
		mManager.name = musicPrefab.name;
		DontDestroyOnLoad (mManager);
	}
}

I want to access the stop music function from another script, so I can’t pass the variable on. What do I do?

In order for a variable to be seen by both methods you have to set the variable at a higher level. That way the variable isn’t localized to a particular method and can be accessed by all methods. Also, I don’t like this approach. I would just set up separate objects with audio sources, one for bg music and one for the menu to play the jingle. Then use audiosource.Stop() and audiosource.Start() in the appropriate order. I haven’t used js for a while sorry, but try this for your current solution:

var mManager;

function Start () 
 {
     currentScore = 0;
     
     if (!GameObject.FindGameObjectWithTag("MM")) {
         mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
         mManager.name = musicPrefab.name;
         DontDestroyOnLoad (mManager);
     }
 }

public static function StopAllAudio() {
         mManager.Destroy(GetComponent (AudioSource));
         for(var audioS : AudioSource in allAudioSources) {
             audioS.Stop();
     }
}

I found the answer. Apperently this works. I just declared a var and set it equal to the one in the program. Also I destroy it and make a new one in the new menu so it works. This is the new code.

function Update () 
{
	if (!GameObject.FindGameObjectWithTag("MM") && CreateMusic == true) {
		var mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
		mManager.name = musicPrefab.name;
		if(mManager.GetComponent.<AudioSource>().enabled == false) {
			mManager.GetComponent.<AudioSource>().enabled = true;
		}	
		MusicManager = mManager;
	}
	//mManager.GetComponent.<AudioSource>().volume = MainMenu.Volume;
}

public static function StopAllAudio() {
	GameMaster.CreateMusic = false;
	GameMaster.MusicManager.GetComponent.<AudioSource>().enabled = false;
	for(var audioS : AudioSource in allAudioSources) {
    	audioS.Stop();
    }
}

and this is code from a different script that helped.

function Update () 
{
	if (!GameObject.FindGameObjectWithTag("MM") && CreateMusic == true) {
		var musicManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
		musicManager.name = musicPrefab.name;
		if(MainMenu.Volume) {
			musicManager.GetComponent.<AudioSource>().volume = MainMenu.Volume;
		}
		if(musicManager.GetComponent.<AudioSource>().enabled == false) {
			musicManager.GetComponent.<AudioSource>().enabled = true;
		}	
		musicMenu = musicManager;
	
	}
}

Thank you all for helping.