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 sharonv · Jul 05, 2021 at 08:31 PM · beginnerwaitforsecondssound effectsaudio.playoneshot

Sound plays multiple times after StartCoroutine when it should only play once.

I've made a power up to my Breakout styled game where you get 200 points instead of 100 points when you hit a break for 10 seconds. I created an IEnumerator which yield returns a WaitForSeconds(10f) and plays a "Ding" sound when it ends. Then, I added a StartCoroutine in OnCollisionEnter2D because that is the function which destroys the bricks and calculates the points. However, while the power up works as it should, the "Ding" sound plays multiple times when it should only play once. I am new to IEnumerators and Coroutines and so I can't really tackle this problem by my own. I would love it if you helped me, thank you.

 void OnCollisionEnter2D(Collision2D collider)
     {
 
         if (collider.gameObject.tag == "brick")
         {
             ad.PlayOneShot(breakBrick);
             int num = Random.Range(0, 10);
             if (num == 9)
             {
                 Instantiate(extra, collider.gameObject.transform.position, collider.gameObject.transform.rotation);
             }
             if ((num == 7 || num == 8) && !isX2)
             {
 
                 Instantiate(x2, collider.gameObject.transform.position, collider.gameObject.transform.rotation);
             }
             Destroy(collider.gameObject);
             if (isX2)
             {
                 StopCoroutine(X2());
                 StartCoroutine(X2());
                 points = points + 200;
             }
             else
             {
                 points = points + 100;
             }
             numBroken++;
         }
 
     }
     IEnumerator X2()
         {
             yield return new WaitForSeconds(10f);
             ad.PlayOneShot();
             isX2 = false;
     
         }
Comment
Add comment · Show 2
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 Devster2020 · Jul 06, 2021 at 04:09 PM 0
Share

Hello @sharonv , it's possible that you have more than one X2 object and hit twice o more of then? In this case, you will have multiple "X2()" execution because using "StopCoroutine" only work on its gameObject.. if you have multiple instance, it can't work on others..

avatar image logicandchaos · Jul 08, 2021 at 03:32 PM 0
Share

try checking if the sound is already playing before you play it.

0 Replies

· Add your reply
  • Sort: 

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

137 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

Related Questions

Waitforseconds causing erratice flickering between active/not active objects 0 Answers

C# Wait For Seconds 3 Answers

Need help applying gravity using C# 2 Answers

Why does my sphere object stop moving on collision? 1 Answer

Roll a ball tutorial Problem Please Help 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