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 ratmstein · Dec 03, 2013 at 05:44 PM · audiosoundplayoneshot

Why will my audio not play in if statement?

I have looked around and cannot seem to find anyone with a similar problem, and I have tried many different ways to get my audio to play. I have a sphere that destroys when it is hit with another sphere using onCollisionEnter. That part works fine. Where i'm having issues is getting my audio clip to play when this happens. I have made one of the spheres an audio source, currently do not have the clip stored in the clip attribute of the audio source. I can get the clip to play by calling audio.PlayOneShot() in the Start() function, but not in my onCollisionEnter. Here is my C# code.

 void OnCollisionEnter(Collision collision) {
         if(collision.gameObject.tag == "Dart") {
             audio.PlayOneShot(Pop, 1f);
             Destroy(collision.gameObject);
             Destroy(gameObject);
         }

Any help is greatly appreciated, Corey

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
1

Answer by amaceika · Dec 03, 2013 at 06:32 PM

You may want to destroy the object after you play the sound. So a simple solution would be a coroutine

 IEnumerator Kill(float time)
 {
    yield return new WaitForSeconds(time);
    Destroy(gameObject); 
 } 

this is a simple sample. You can look at the reference page here.

 void OnCollisionEnter(Collision collision) {
        if(collision.gameObject.tag == "Dart") {
          audio.PlayOneShot(Pop, 1f);
          Destroy(collision.gameObject);
          StartCoroutine("Kill", 1f);  
        }

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 ratmstein · Dec 03, 2013 at 09:22 PM 0
Share

amaceika, I am still not getting a sound using your suggestion. I have used coroutines before, but I may not be doing something right. This is what I have now

 void OnCollisionEnter(Collision collision) {
         if(collision.gameObject.tag == "Dart") {
             audio.PlayOneShot(Pop, 2f);
             Destroy(collision.gameObject);
             StartCoroutine("$$anonymous$$ill", 2f);
             //Destroy(gameObject);
         }
         
         if(collision.gameObject.tag == "Player") {
             //Destroy(collision.gameObject);
             Destroy(gameObject);
         }
             
     }
 
     IEnumerator $$anonymous$$ill(float time) {
         yield return new WaitForSeconds(2);
         Destroy(gameObject);
     }

Have I done something incorrect?

avatar image amaceika · Dec 03, 2013 at 09:33 PM 0
Share

On which object does the clip exist? Because if it's on the collision object it will not play due to it being destroyed, if it is on the object that is in the Ienum it should technically work. I would also make sure that the play on start check box in the editor is off. If this doesn't work you may have to go about playing your sound differently like the other answer suggests. Write a class that all it does is play the sound clips based on when you need them to play. if it comes to that i can help yah write something quick.

avatar image
0

Answer by thaiscorpion · Dec 03, 2013 at 05:52 PM

It won't play the audio because you are destroying the object before it plays. Try to play the audio from an object that won't be destroyed, like an audioController or something like that.

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

I need help about audio.PlayOneShot!! 1 Answer

How to stop an Audio Clip without an Audio Source? 1 Answer

AUDIO ISSUES - PlayOneShot... is cutting short... I think? 2 Answers

Sound distorted on impact. 1 Answer

Making a sound play only once with a Boolean variable in 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