x


Audio won´t stop

Hi! I have a level (2) where you choose music to be played in the next level (3). You can return back to this level (2) with a (GUI button BACK) in lvl 3. The back GUI button sends a message "ShutDownSound" (to this object and this code where all music is stored in vars) before lvl (2) is loaded. I am very new to coding so this may not be good code, but it mainly works (I get the random sound playing as intended for lvl 3), I can return back the FIRST time and in that case the music stops (as expected), but if I goto lvl 3 again and return a SECOND time, then the music keeps playing.. whyyyy? :-(

// This code is put on an object in LVL 2, with DontDestroyOnLoad code.

function shutDownSound (){audio.Stop();}

// called from another (levels BACK button) when returning here. // called before Application.LoadLevel ("2");

function Awake () { DontDestroyOnLoad (transform.gameObject);}

var rmusik : int; // Defines by a number which type of music is played.

function musik1(){
rmusik = 1;
rCallmusik(); } // called with sendmessage from a GUI script. "Rock music".

function musik2(){
rmusik = 2; rCallmusik(); } // called with sendmessage from a GUI script. "Pop music".

var sound1: AudioClip; var sound2: AudioClip; var sound3: AudioClip; var sound10: AudioClip; var sound11: AudioClip; var sound12: AudioClip;

var rmusikTrack : int;
// a var to temporary store a randomly chosen song.

function rCallmusik (){ if (rmusik==1){ rmusikTrack = Random.Range(1,4); if (rmusiktrack==1){ if (audio.isPlaying) return; audio.clip = sound1; audio.Play();}

if (rmusikTrack==2){ if (audio.isPlaying) return; audio.clip = sound2; audio.Play();}

if (rmusikTrack==3){ if (audio.isPlaying) return; audio.clip = sound3; audio.Play();} }

if (rmusik==2){ rmusikTrack = Random.Range(10,13); if (rmusikTrack==10){ if (audio.isPlaying) return; audio.clip = sound10; audio.Play();}

if (rmusikTrack==11){ if (audio.isPlaying) return; audio.clip = sound11; audio.Play(); }

if (rmusikTrack==12){ if (audio.isPlaying) return; audio.clip = sound12; audio.Play(); } }

// The AudioSource attached to this object has LOOP unchecked in the inspector.

more ▼

asked Feb 29 '12 at 12:39 PM

Tommy 1 gravatar image

Tommy 1
22 3 4 6

(comments are locked)
10|3000 characters needed characters left

0 answers: sort oldest
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1030
x143

asked: Feb 29 '12 at 12:39 PM

Seen: 375 times

Last Updated: Feb 29 '12 at 12:39 PM