Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by brewster2110 · Apr 12, 2018 at 08:57 AM · audiotriggerscript.audiosource

Wanting audio to pause on trigger enter

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Lukas-Wendt · Apr 18, 2018 at 04:07 PM

If the first script is EnterDarknessScript (you should paste the class name with the code so it is easier for us to talk about), you could create a method in EnterDarknessScript called StopAudio. This method could simply do:

 public void StopAudio()
 {
     GetComponent<AudioSource>().Stop();
 }


Then you could call this method from your second script like so:

 public void OnTriggerEnter(Collider collider)
 {
     DeathArea.GetComponent<EnterDarknessScript>().StopAudio();
 }


You should maybe save a reference to the AudioSource, so you don't need to call GetComponent all the time.


If you want to pause the audio, you can just use the Pause method on the AudioSource.


You can see all the available methods on AudioSources in the documentation here: https://docs.unity3d.com/ScriptReference/AudioSource.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio play when enter trigger 1 Answer

Trying to make a splitscreen game, having issues with the AudioSource and distance between objects. 0 Answers

Stop AudioSource on Trigger!!! 1 Answer

How to use a key as trigger? 2 Answers

Dialogue Problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges