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 Mr.BMS · Jan 05, 2015 at 02:46 PM · javascriptaudiosound on key

Audio is repeating.

Hi!Can you please fix my code?My problem is when you click on text(with box collider) audio sonds,but if you click it 2x or more fast same audio sounds many times.I want to make,when you press one of the text(which plays audio)you can't press it again when it sounds or other audio.**

I need to make that when audio sounds,you can't turn on other audios until you 1st stops to sound.And if audio is pressed and you press it again,it stops.


  #pragma strict
  
  var isQuitButton = false; 
  
  var sound : AudioClip;
  
  function OnMouseDown()
  {
  renderer.material.color = Color.red;
  audio.PlayOneShot(sound);
  waitForAudio(audio.clip.length);
  
  }
  function waitForAudio( time:float){
  yield WaitForSeconds(time);
  //change color back
  renderer.material.color = Color.white;
  }

Comment
Add comment · Show 1
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 Jonesy19 · Jan 05, 2015 at 03:32 PM 0
Share

If I understand your question correctly, you want to click the text and have audio play, and while the audio is playing, clicking the text will not play any more audio. You can do this by storing the last time the text was clicked up top. Then, when the text is clicked again, simply do:

if (Time.time > lastTimeClicked + audio.clip.length) { audio.PlayOneShot(sound); }

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Priyanshu · Jan 05, 2015 at 03:44 PM

Just add a bool which turns true when 'yield WaitForSeconds(time)' finishes.

And call 'WaitForSeconds' and 'audio.PlayOneShot' when that bool is true.

Try this:

 var soundPlaying = false;
 
     function OnMouseDown()
     {
         if( !soundPlaying)
         {
             renderer.material.color = Color.red;
             audio.PlayOneShot(sound);
             waitForAudio(audio.clip.length);
             soundPlaying = true;
         }
     }
     function waitForAudio( time:float){
         yield WaitForSeconds(time);
         //change color back
         renderer.material.color = Color.white;
         soundPlaying = false;
     }
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
avatar image
0

Answer by StephanM · Jan 05, 2015 at 03:34 PM

Try checking to see if it's already playing before allowing another click

function OnMouseDown() { if(!audio.isPlaying) { renderer.material.color = Color.red; audio.PlayOneShot(sound); waitForAudio(audio.clip.length); } }

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
avatar image
0

Answer by adelphiaUK · Jan 05, 2015 at 03:51 PM

Different people have different approaches. First question is, do you have multiple or a single sound attached to the game object?

If you have a single audio source then I would use audio.Play() and make sure looping is disabled on the actual source.

 function OnMouseDown() {
     if(!audio.isPlaying) {
         renderer.material.color = Color.red;
         audio.Play();
         waitForAudio(audio.clip.length);
     } else {
         waitForAudio(0.5); // ensure audio has completed
     }
 }
 
 
 function waitForAudio(duration : float) {
     if(audio.isPlaying) {
         yield WaitForSeconds(duration);
     }
     renderer.material.color = Color.white;
 }
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

6 People are following this question.

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

Related Questions

How to prevent an audio clip being spammed? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Scale messing with code 0 Answers

How do I stop footstep sound from playing when I release 'w'? 1 Answer

Sound wont play when triggered (javascript) 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