Music works but not when accessed by another script in JavaScript

So I’m working on a small puzzle game and I have a game object with a script that handles loading different levels and changing the music based on if the player is on the title screen or one of the various levels. In that same script, I currently have GUI buttons in the OnGUI() function that changes the levels and music flawlessly by changing some variables and running the Start() function again. I want to change from GUI buttons to other game object buttons, and that works for changing the level but I get an error “Can not play a disabled audio source” and the music tracks do not change.

Here is the code for my Music() function, basically it stops and plays music based on variables

function Music()
{
	if(titleScreen == true) //checks to see if the title screen music is already playing
	{
		if(levelCounter == 0)//checks to see if level counter set to the title screen
		{
			if(playMusic == 0)
			{
				titleSong.Play();//plays the titlescreen song
			}
		}
		
		else if(levelCounter > 0 )//checks to see if the level 1 or higher
		{
			if(playMusic == 0)
			{
				titleSong.Stop();//stops the title song
				firstSong.Play();//plays the first level set song
			}
				titleScreen = false; //sets title screen to false
				levelSet1 = true; //sets the first level set to true
		}
	}
		
	else if(levelSet1 == true) //checks to see if the first level set music is already playing
	{
		//this is used to update the game's music based on the level selected
		if(levelCounter == 0)//checks to see if the game is on the title screen
		{
			if(playMusic == 0)
			{
				firstSong.Stop();//stops the first level set song
				titleSong.Play();//plays the title song
			}
			
				levelSet1 = false; //sets the first level set to false
				titleScreen = true; //sets title screen to true	
		}
	}
}

In that same script, I have this code located in the Start() function and the audio sources are attached to this object.

	var gameMusic = GetComponents(AudioSource); //puts the audio sources in an array
	titleSong = gameMusic[0]; //title screen music is the first song attached
	firstSong = gameMusic[1]; //first level set music is the second song attached
	Music(); //executes the music function to play the right music

As mentioned before, that works perfectly, but I get the error when I have the code from another script running the start function from that script. I have the first script created as a variable and then have it run that Start() function in OnMouseDown().

var gridArrayScript : scr_gridArray;
function OnMouseDown() 
{
	
	gridArrayScript.levelCounter = 2; //sets the level to 2 before running the level creation function
	gridArrayScript.Start(); //runs the start function in the grid array script to reset the level
}

So is there something else I need to do in order to start/stop audio from another object? I didn’t expect to run into an issue because it’s the functions in the original script that are executing the commands, the second script is only telling it to run Start()

Hi, again. Got insomnia party because my new PC is arriving tomorrow(like Christmas) and party because I knew as soon as I lay down that the code I gave you could have been a lot better, so here is my new answer.

The Main Music functions :

//State Enums collections.
enum eMusic_States { Title_Screen, Music_1, Music_2, Music_3}
public var sThe_Music_State_Is : eMusic_States;

//Mutes var.
private var bMusic_Is_Muted : boolean = false;
 
function subPlay_Music() {
 
    //Stop All music.
    subStop_Music();
 
    //Start Playing the correct music.
    switch (sThe_Music_State_Is) {
 
        case eMusic_States.Title_Screen:
            oTitle_Music_Source.audio.Play()
        break;
 
        case eMusic_States.Music_1:
            oMusic_Source_1.audio.Play();
        break;
 
        case eMusic_States.Music_2:
            oMusic_Source_2.audio.Play();
        break;
 
        case eMusic_States.Music_3:
            oMusic_Source_3.audio.Play();
        break;
    }
}

function subStop_Music() {

    //Stop All music.
    oTitle_Music_Source.audio.Stop()
    oMusic_Source_1.audio.Stop();
    oMusic_Source_2.audio.Stop();
    oMusic_Source_3.audio.Stop();
}

Called like this when you change your level :

function subLevel_Has_Changed_Music_State_Change() {

    //When level changes... (With 0 for Title Screen)
    if (iLevel_Number == 0) {
 
        sThe_Music_State_Is = eMusic_States.Title_Screen;

        if (bMusic_Is_Muted == false) {
            subPlay_Music();
        }
 
    } else if (iLevel_Number == 1) {
 
        sThe_Music_State_Is = eMusic_States.Music_1;
        
        if (bMusic_Is_Muted == false) {
            subPlay_Music();
        }
 
    } else if (iLevel_Number == 10) {
 
        sThe_Music_State_Is = eMusic_States.Music_2;
        
        if (bMusic_Is_Muted == false) {
            subPlay_Music();
        }
 
    } else (iLevel_Number == 20) {
 
        sThe_Music_State_Is = eMusic_States.Music_3;
        
        if (bMusic_Is_Muted == false) {
            subPlay_Music();
        }
    }
}

With this for mute :

function subMute_Music() {
 
    bMusic_Is_Muted = true;
    subStop_Music();
}

And this for Un-Mute :

function subUnMute_Music() {

    bMusic_Is_Muted = false;
    subPlay_Music();
}

This should do the job for you far better, sorry about the long winded rubbish above. My thinking cap had gone to sleep :smiley: