- Home /
 
               Question by 
               unity_2003el612003fy9 · Dec 27, 2018 at 01:09 AM · 
                c#audiotriggerscript.  
              
 
              Script to trigger all audio playing to stop, and play a different audio?
I have this scene where the player enters a room and this NPC is triggered to chase after the player.
What I want to do is script this invisible cube at the doorway, where the player collides through it and triggers all audio playing at that current moment to stop, and play a different audio (named 'chase') instead.
I can't really program C# for the life of me, some help would really be appreciated.
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by ray2yar · Dec 27, 2018 at 02:26 AM
This code effectively will turn all sounds off by muting all audio sources. You will need to specify which audio source to unmute and play your clip using the inspector.
     public AudioSource musicPlayer; //the single audio source to turn back on
     public AudioClip newSong; //the clip you want to play
 
     void OnTriggerEnter(Collider col)
     {
         if (col.tag != "Player") return;
         foreach(AudioSource aud in FindObjectsOfType<AudioSource>())
         {
             aud.mute = true;
         }
         musicPlayer.clip = newSong;
         musicPlayer.mute = false;
     }
     
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                