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()