- Home /
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064617im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pause Menu Audio 1 Answer
How to stop Audio not all?? 0 Answers
java.lang.IllegalStateException: stop() called on uninitialized AudioTrack. 1 Answer
How to Stop all audio 10 Answers
How to stop audio 1 Answer