Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sido · Feb 02, 2014 at 03:59 PM · javascriptaudiotimer

Stop audio playing after a timer reset

Hi I am very new to all this and have tried to solve most of my problems myself but I'm stuck on how to add in a 2nd condition to this Jscript.

I use 3 scripts.

I have an add time script which adds +10 seconds of time on an item pickup to two other scripts one counts down to 0 and restarts the level and one plays a audio file at 0 seconds but the countdown is 7 so the audio plays with 3 seconds of the level time left.

I want to add a condition into the audio script so the sound is stopped if the timer goes back above 0 (3 seconds level time)

I originally had 1 timer script but again got stuck on how the 'if' conditions worked. This solved one problem but created another.

Thanks in advance.

 #pragma strict
 var countdown : AudioClip;
 var timer2 : float = 7.0;
 
 
 function Update()
 {
     timer2 -= Time.deltaTime;
     
     
     if(timer2 <= 0)
     
     {
     
     timer2 = 0;
     AudioSource.PlayClipAtPoint(countdown, transform.position);
     Destroy(gameObject);
         
             
         
     }
 }
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

2 Replies

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

Answer by markmmiller · Feb 02, 2014 at 04:05 PM

Switch your if statements around like this. That way your application will stop the audio until the timer is up. If the timer jumps back up above zero by any amount (1, 2 or 10 seconds etc) it should automatically stop the audio. I doubt it will be a graceful stop but you can investigate this yourself later.

Loop is below

 if(timer2 > 0.0f)
 {
    AudioSource.Stop();
 }
 else if(timer <= 0.0f)
 {
   AudioSource.PlayClip etc..
 }
Comment
Add comment · Show 2 · 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 sido · Feb 02, 2014 at 06:30 PM 0
Share

Thanks that helps, the Loop at least works after changing some elements. However if I use Destroy(gameObject) the object is obviously removed and the sound doesn't play. If I don't use Destroy(gameObject)the audio layers up and plays over itself in the loop. Is there anyway I can make the audio not play if it is currently playing ? I've had a look at "if (!audio.isPlaying)" but can't get it to work.

Here is the current code:

 function Update()
 {
     timer2 -= Time.deltaTime;
     
     if(timer2 > 0.0f)
     {audio.Stop();    
     }
     
     else if(timer2 <= 0.0f)
     {audio.PlayOneShot(countdown, 0.7);    
      Destroy(gameObject);
     }
 }
avatar image markmmiller · Feb 02, 2014 at 08:14 PM 0
Share

I'm not sure I understand what it is you are trying to do. What object are you needing to destroy?

avatar image
1

Answer by sido · Feb 02, 2014 at 09:36 PM

Managed to solve this myself.

     if(timer2 > 0.0f)
     {
         audio.Stop(); 
     }
  
     else if(timer2 <= 0.0f)
     {
  if (!audio.isPlaying){
             audio.clip = countdown;
             audio.Play();
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

19 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

Related Questions

Timer Scripting No Idea On Script 0 Answers

Need help with delaying a timer (javascript) 2 Answers

Countdown 2 Answers

Play Audio at the end of a Countdown 0 Answers

Convert this string formatting from C# to JS 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