I’ve been futzing around with the survival shooter after the tutorial, and I added a damage chain score. I wanted to switch between the background songs once the damage chain gets high enough (from the original song to “Enter Sandman”).
However, I ran into a problem: I couldn’t attach the two audio sources to the background music component in the hierarchy without hiding the one of the audio sources.
I think the simplest way to explain the problem is to post my configuration and my code:
And here is my code (which, obviously, does not work right now–actually, it doesn’t play anything at all…so it may be that the way I’m structuring the code is incorrect as well)…
using UnityEngine;
using System.Collections;
public class SongManager : MonoBehaviour {
AudioSource regular_music;
AudioSource high_dmg_music;
// Use this for initialization
void Start () {
regular_music = GetComponent<AudioSource> ();
high_dmg_music = GetComponent<AudioSource> ();
regular_music.loop = true;
regular_music.Play ();
high_dmg_music.loop = false;
high_dmg_music.Stop ();
}
// Update is called once per frame
void Update () {
if (DamageManager.dmg_chain > 1500) {
regular_music.loop = false;
high_dmg_music.loop = true;
high_dmg_music.Play ();
} else {
if (high_dmg_music.isPlaying) {
high_dmg_music.loop = false;
high_dmg_music.Stop ();
regular_music.loop = true;
regular_music.Play ();
}
}
}
}
How do I grab the correct audio sources and get the songs to toggle back and forth? (And any advice on how to make “Enter Sandman” play over the sound effects (and play louder) than the original background music would be huge too…since I want the player to be able to appreciate the atmosphere change, while they will be basically holding down the trigger (I made some changes to the spawn rate, firing speed, area of effect, etc, so there are a LOT of zombunnies and bears out there…)).