Stop audio when entering certain scenes

Allright, here’s the deal:

Every scene, Unity creates a Music Clone (empty GameObject with Audio Listener added), which has the following script added:

function Awake()
{
  GameObject.DontDestroyOnLoad(this);
}

So, when you enter a scene (or reset the same scene), the audio won’t stop, but continue to play.

HOWEVER!
In some scenes, the audio has to change (Joyful scene > joyful music. Scary scene > scary music). BUT! With this music-cloning, only one kind of music keeps playing in every scene (the joyful music). How do I create something that says: In this scene, DO NOT clone the music, but play (Scary music) instead!

PS.
My progamming-teacher created the Audio-cloning-thingy, so I don’t really know how he made that :stuck_out_tongue: Wasn’t paying much attention. Besides, I’m pretty much of a scripting-noob.

Deadline is tomorrow, 13:30, so please, someone help me! :shock:

Thanks!

Anyone, please? It’s getting pretty late here in Holland :smile:

Well, if you’re just using one music object for all the scenes, then it’s simple. Put this into the script on the music object:

var myMusic : AudioClip[];
var curMusic : int = 0;
var mySource : AudioSource;
var changing : boolean = false;

function SwitchMusic(hit : int)
{
   if (hit != curMusic  !changing) {
      changing = true;
      curMusic = hit;
      while (mySource.volume > 0) {
         mySource.volume -= Time.deltaTime;
         yield;
      }
      mySource.Stop();
      mySource.clip = myMusic[hit];
      mySource.Play();
      while (mySource.volume < 1) {
         mySource.volume += Time.deltaTime;
         yield;
      }
      changing = false;
   }
}

Drag all your music into the myMusic array, drag the game object with the audio source onto the mySource variable, and have a script in each scene call the new function inside the object with the audio source. All you have to do is pass it the integer for the position of the new song.