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 /
avatar image
0
Question by brewster2110 · Apr 13, 2018 at 03:25 AM · audiotriggerscript.component

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

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 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;
         }

Comment
Add comment · Show 1 · 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
avatar image brewster2110 · Apr 13, 2018 at 04:12 AM 0
Share

Thank you so much

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

115 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

Related Questions

Script to trigger all audio playing to stop, and play a different audio? 1 Answer

OnTriggerExit stop audio 1 Answer

how do i play a sound if i press a particular key? 2 Answers

Play sound only once 2 Answers

Play AudioClip on trigger with First Person Controller 1 Answer


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