I’m having issues making a system where the background music changes whenever you enter a separate area. For example, if you move to the right side of a room the music should be different than what is playing on the left side of the room.
I currently have a system which uses two scripts: one that is attached to a global MusicManager game object and another connected to numerous triggers (which have AudioSource components attached) set around the room. What I hope to accomplish is this:
- If no song is playing and player enters a music trigger, then play that music
- If music is playing and player enters a music trigger, stop currently playing music and start new music (attached to currently entered trigger)
However, the system I have in place plays the song connected to each trigger without stopping the previous song from playing.
The main issue is coming from the script attached to each audio trigger, as it just plays the attached song whenever the player enters it. My initial design was to keep it simple, so that’s why it looks like this:
public class MusicSelect : MonoBehaviour {
GameObject player;
void Start() {
player = GameObject.FindGameObjectWithTag("Player");
}
void OnTriggerEnter(Collider other) {
if (other.gameObject == player) {
audio.Play();
}
}
}
All of the major work was supposed to be handled by the MusicManager’s script, which would turn the volume down to 0 and switch the playing song to the one from the current audio trigger. Here’s the code for the MusicManager:
public class MusicManager : MonoBehaviour {
public float musicFadeSpeed = 1.8f;
GameObject[] musicTriggers;
AudioSource currAudio, newAudio;
static MusicManager instance = null;
bool changingSongs = false;
void Start()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
musicTriggers = GameObject.FindGameObjectsWithTag("MusicTrigger");
}
void Update() {
MusicSelect selection = GameObject.FindGameObjectWithTag("MusicTrigger").GetComponent<MusicSelect>();
if(!selection.audio.isPlaying)
currAudio = selection.audio;
}
}
If anyone has any useful and relevant information about this problem, it would be much appreciated.