- Home /
Wanting audio to pause when entering a trigger zone
I'm very new to Unity so the fix to my issue is probably very straightforward.
I have two scripts, both linked to in-game objects which are used as trigger areas. On my first game object/script, I have an audio source. I want to make it so that when the player enters the second trigger area it stops the audio source from the first one, or pauses it. I don't want to completely get rid of it on trigger enter as it is used multiple time throughout the game. Just stop it so I can start it again later. (I already have some code in these (Not related to the issue))
Object/Script one:
 {
     public AudioClip BuildUp;
     public float timeInterval = 1;
     public float counternoise;
     void Start()
     {
         counternoise = 0;
         GetComponent<AudioSource>().playOnAwake = false;
         GetComponent<AudioSource>().clip = BuildUp;
     }
     //Plays the sound whenever in trigger area
     public void OnTriggerEnter(Collider collider)
     {
         counternoise = 0;
     }
     public void OnTriggerStay(Collider collider)
     {
         counternoise += Time.deltaTime;
         if (counternoise > timeInterval)
         {
             //Die
             GetComponent<AudioSource>().Play();
             counternoise = -100;
         }
      
     }
And the second one (one which stops audio from first on entry)
 {
     public GameObject DeathArea;
     public void OnTriggerEnter(Collider collider)
     {
         DeathArea.GetComponent<EnterDarknessScript>().counternoise = 0;
     }
     public void OnTriggerStay(Collider collider)
     {
         DeathArea.GetComponent<EnterDarknessScript> ().counternoise = 0;
     }
 }
Thanks a bunch for any help
Answer by x4637x · Apr 13, 2018 at 03:45 AM
I assume this EnterDarknessScript is the class name of your first script. And inside there you should have a variable reference to your AudioSource instead of use GetComponent<AudioSource>() every time. Change to first script :
         public AudioClip BuildUp;
         public AudioSource AudioPlayer;
         public float timeInterval = 1;
         public float counternoise;
         void Start()
         {
             counternoise = 0;
             AudioPlayer = GetComponent<AudioSource>();
             AudioPlayer.playOnAwake = false;
             AudioPlayer.clip = BuildUp;
         }
         //Plays the sound whenever in trigger area
         public void OnTriggerEnter(Collider collider)
         {
             counternoise = 0;
         }
         public void OnTriggerStay(Collider collider)
         {
             counternoise += Time.deltaTime;
             if (counternoise > timeInterval)
             {
                 //Die
                 AudioPlayer.Play();
                 counternoise = -100;
             }
         }
And for your second script :
         public GameObject DeathArea;
         public void OnTriggerEnter(Collider collider)
         {
             DeathArea.GetComponent<EnterDarknessScript>().counternoise = 0;
             DeathArea.GetComponent<EnterDarknessScript>().AudioPlayer.Pause();
         }
         public void OnTriggerStay(Collider collider)
         {
             DeathArea.GetComponent<EnterDarknessScript>().counternoise = 0;
         }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                