Hey everyone,
I approached finishing my game and I’m trying to implement a playlist of 4 tracks that would play randomly throughout the game when the “mute music” button is not checked. So far the playlist works fine when the game stays in the main menu. However, when I change scenes ( when I go from menu to actual gameplay ) the track that is currently played keeps working but there is no other that would play after it, it’s like the playlist just stops at one track.
There is a “GlobalObject” game object that handles tracks and other data that is transmitted from scene to scene ( like the color of the player which can be configured via the settings menu, a ui panel found in the main menu scene ). An instance of this object is found in both the main menu and the game level scenes.
//You can ignore anything related to myColor or isMuted, they work fine
using UnityEngine;
using System.Collections;
public class GlobalControl : MonoBehaviour
{
public static GlobalControl Instance;
//Definition of the four tracks
public AudioClip septemberSky;
public AudioClip shadowsOfTheMind;
public AudioClip danceOfThePixies;
public AudioClip rememberTheDreams;
//Color to be assigned to the player
public Color myColor;
//To check if sound is muted
public bool isMuted;
public int ranTrack; //The track that is currently being played
public int nextRanTrack; //A random number that would determine the next track, in a way that the same track does not repeat itself successfully
public void Start()
{
isMuted = false;
AudioSource audio = GetComponent<AudioSource>();
myColor = Color.white;
TrackHandler();
SaveData();
}
public void ChoseTrack() //This method chooses what track to play then calls PlayTrack()
{
ranTrack = (int) Random.Range( 0, 4 );
if ( ranTrack == 0 )
{
GetComponent<AudioSource>().clip = septemberSky;
PlayTrack();
}
else if ( ranTrack == 1 )
{
GetComponent<AudioSource>().clip = shadowsOfTheMind;
PlayTrack();
}
else if ( ranTrack == 2 )
{
GetComponent<AudioSource>().clip = danceOfThePixies;
PlayTrack();
}
else
{
GetComponent<AudioSource>().clip = rememberTheDreams;
PlayTrack();
}
}
public void PlayTrack() //This method plays the actual track then calls WaitForNextTrack()
{
GetComponent<AudioSource>().Play();
StartCoroutine( "WaitForNextTrack" );
}
public IEnumerator WaitForNextTrack() //This method decides which track should be played next then calls ChoseTrack()
{
if ( ranTrack == 0 )
{
yield return new WaitForSeconds( 201 );
nextRanTrack = (int) Random.Range( 1, 4 );
if ( nextRanTrack == 1 )
{
ranTrack = 1;
ChoseTrack();
}
else if ( nextRanTrack == 2 )
{
ranTrack = 2;
ChoseTrack();
}
else if ( nextRanTrack == 3 )
{
ranTrack = 3;
ChoseTrack();
}
}
else if ( ranTrack == 1 )
{
yield return new WaitForSeconds( 199 );
nextRanTrack = (int) Random.Range( 1, 4 );
if ( nextRanTrack == 1 )
{
ranTrack = 0;
ChoseTrack();
}
else if ( nextRanTrack == 2 )
{
ranTrack = 2;
ChoseTrack();
}
else if ( nextRanTrack == 3 )
{
ranTrack = 3;
ChoseTrack();
}
}
else if ( ranTrack == 2 )
{
yield return new WaitForSeconds( 207 );
nextRanTrack = Random.Range( 1, 4 );
if ( nextRanTrack == 1 )
{
ranTrack = 0;
ChoseTrack();
}
else if ( nextRanTrack == 2 )
{
ranTrack = 1;
ChoseTrack();
}
else if ( nextRanTrack == 3 )
{
ranTrack = 3;
ChoseTrack();
}
}
else
{
yield return new WaitForSeconds( 153 );
nextRanTrack = Random.Range( 1, 4 );
if ( nextRanTrack == 1 )
{
ranTrack = 1;
ChoseTrack();
}
else if ( nextRanTrack == 2 )
{
ranTrack = 2;
ChoseTrack();
}
else if ( nextRanTrack == 3 )
{
ranTrack = 0;
ChoseTrack();
}
}
}
public void TrackHandler() //A method that handles the playlist behavior
{
ChoseTrack();
PlayTrack();
StartCoroutine( "WaitForNextTrack" );
}
void Awake() //To check that only one instance of the global object is present in the scene
{
if ( Instance == null )
{
DontDestroyOnLoad( this.gameObject );
Instance = this;
}
else if ( Instance != this )
{
Destroy( gameObject );
}
}
public void SaveData() //To transmit data from a scene to another
{
GlobalControl.Instance.myColor = myColor;
GlobalControl.Instance.ranTrack = ranTrack;
GlobalControl.Instance.nextRanTrack = nextRanTrack;
}
}
My guess is that the problem lies in the TrackHandler() method as it calls methods that already call each other, or maybe it isn’t the best strategy to call TrackHandler() in Start().
I hope you guys can help me.
Merry Christmas!