• 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 LukaKotar · Aug 14, 2012 at 02:37 PM · updatefunctionstrangeaudio.playoneshotbehavior

audio.PlayOneShot() + Update() = mess?

Hi.

I've been having trouble getting audio.PlayOneShot() to play only once, and not overlap with the same sound each frame, cause Update() is getting called each frame. The thing I want to do is to play a sound once a boolean is true.

Things I've tried:

  1. I created a new boolean variable, and deactivated it after the audio.PlayOneShot() line (if the boolean is true, play a sound, after that line, deactivate it)

  2. I created a boolean function, and play the sound after the "return true;" line

  3. Call it in another (regular void) function (same result, the Update() function is still calling the newly created function each frame),

  4. I have also tried to Invoke() that function.

How could I play a sound when the boolean mentioned in the beginning is turned true?

Comment
Add comment · Show 3
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 LukaKotar · Aug 14, 2012 at 02:45 PM 0
Share

O$$anonymous$$, I'll try that.

avatar image LukaKotar · Aug 14, 2012 at 02:48 PM 0
Share

Thanks, but I got the same result...

avatar image Remingsworth · Apr 02, 2015 at 05:38 AM 0
Share

I'm not sure why but the code you posted here DESTRU$$anonymous$$TORR has the initial bool set to FALS$$anonymous$$ I tried using your code and it didn't work. I'm new to Unity and scripting so I couldn't get your code to work until I set the initial bool to TRU$$anonymous$$ Can someone explain this? This is what is working for me currently in my game:

       AudioSource laserAudio;
       bool audioEnabled = true;
       
       void Awake () {
               laserAudio = GetComponent<AudioSource>();
           }
       void Update () {
               if (Input.GetButton ("Fire1")) {
                   if(audioEnabled)
                   {
                       laserAudio.Play ();
                       audioEnabled=false;
                   }
               } else {
                   laserAudio.Stop ();
                   audioEnabled=true;
               }
       }

 

The only difference in my example is that I am not calling the sound once because it's a continuous loop while holding left-click and then I reset the bool to true once I release left-click.

1 Reply

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

Answer by DESTRUKTORR · Aug 14, 2012 at 02:48 PM

C# version:

 bool playAudio=false;
 void Update()
 {
     if(playAudio)
     {
         audio.PlayOneShot();
         playAudio=false;
     }
 }

Javascript version:

 var playAudio = false;
 function Update()
 {
     if(playAudio)
     {
         audio.PlayOneShot();
         playAudio=false;
     }
 }

Just add the script that isn't in your file to the appropriate areas.

Comment
Add comment · Show 7 · 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 LukaKotar · Aug 14, 2012 at 02:52 PM 0
Share

Looks like when I first tried this, I probably did something wrong. Thanks! Works!

avatar image TheVectorHunter · Aug 14, 2012 at 02:55 PM 0
Share

so you had the answer all along Luka$$anonymous$$otar....

avatar image LukaKotar · Aug 14, 2012 at 02:59 PM 0
Share

@TheVectorHunter Well yeah... If you read the question, you'll see that I mentioned this in it. LOL, I've seen this answer, and I thought: oh well, I guess I'll try it again... ;)

avatar image DESTRUKTORR · Aug 14, 2012 at 05:12 PM 0
Share

I wasn't sure if this was what you meant, but I knew it should work. I'm glad you got it running :) good luck in your future endeavors!

avatar image pokequality · May 13, 2019 at 06:35 PM 1
Share

I'm confused. How would this solution work? Surely the lines inside the 'if' statement would never be called because playAudio would never be set to true?

avatar image Sha67 pokequality · Sep 26, 2021 at 05:46 PM 0
Share

The question has been posted since 2012, so it's very old(even PlayOneShoot method have been changed), I don't know if it was working at that point in time, but logically it doesn't seem to work. Anyway incase someone is looking for a working code :

     public AudioSource audio;
     public AudioClip clip; // make sure to have a reference in the inspector window
     bool playing; // A private boolean with default value set to false
 
     void Start()
     {
         this.audio.clip = clip;
     }
     private void Update()
     {
         if (!this.playing)
         {
             audio.PlayOneShot(clip);
             StartCoroutine(resetPlayingStatus(this.clip.length)); // Starting a coroutine with a float value of clip length
         }
         this.playing = true; // set playing to true after the clip first playing
 
     }
 
     IEnumerator resetPlayingStatus (float clipLength)
     {
         yield return new WaitForSeconds(clipLength); // Make the coroutine waits until the clip is finished
         this.playing = false; // set playing to false, so the clip plays again
     }

Make sure to include using System.Collections; in your script.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Rigidbody.AddForceAtPosition & functions 1 Answer

Im a bit confused on a simple script 1 Answer

Aiming with function "Update" not working 0 Answers

Update() doesnt call method or modify values 1 Answer

Update not being called 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