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 jadaith · Nov 11, 2013 at 05:17 PM · c#audio

Sound not working with onTriggerEnter

I'm trying to have an audio file play when it's destroyed but its not working.

Here is my code.

I have the audioclip set up here.

     public AudioClip deathSound; 


And here is the code that has the audio file in.

 switch(other.tag)
         {
         case "EnemyProjectile":
 
             
             audio.PlayOneShot(deathSound);
             Instantiate(Explosion, transform.position, Quaternion.identity);
             Destroy(other.gameObject);
             GameManager.PlayerDies();
             Destroy(gameObject);
             break;
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 Tomer-Barkan · Nov 12, 2013 at 07:12 AM 2
Share

Is the audio source a component to one of the objects that you are destroying? If it is, that's why it won't play... a destroyed object no longer exist so it can not play sounds either. You'll need a separate object to play the sound, one that is not being destroyed.

avatar image jadaith · Nov 12, 2013 at 08:26 PM 0
Share

Would it be better to have a separate gameobject that handles all the sounds then?

avatar image amaceika · Nov 12, 2013 at 08:34 PM 0
Share

you could try and destroy when on the completion of the sound playing. you should be able to tell when the clip is playing. once it completes then destroy your objects. then you don't need to have a separate object for sound.

1 Reply

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

Answer by fafase · Nov 12, 2013 at 08:38 PM

@tkbn is leading you to the right path.

You can fix this simply changing which object will play:

 AudioSource.PlayClipAtPoint(deathSound, transform.position);

The good point of that method, it is static, so it does not depend on an object. What it does is create a new object that plays the sound and dies when the sound is over.

Comment
Add comment · Show 3 · 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 Tomer-Barkan · Nov 12, 2013 at 08:55 PM 0
Share

I wasn't familiar with @fafase 's alternative, but it definitely seems to be the best match for your scenario.

avatar image amaceika · Nov 12, 2013 at 08:57 PM 0
Share

Ohh i hadn't thought of that either good one fafase

avatar image jadaith · Nov 13, 2013 at 12:33 AM 0
Share

Thanks for all the replies and help. I'll be trying this out when I get a chance. I didn't know about that one, thanks a lot.

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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Audio: how do I immediately play a new audioclip without delay(code included)? 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Audio Low Pass Filter Over Time Muffled Sounds Indoors 0 Answers


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