Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by Fariz · May 22, 2010 at 12:07 PM · audiodestroysoundstop

Sound stops when object is destroyed

Hi there,

I'm currently trying to build a game that requires the character to pick up some items and I want a sound to play when an item is picked up.

below is the code that I have so far

var sound:AudioSource var CollectSound:AudioClip

function Start(){ sound = GetComponent(AudioSource); }

function OnTriggerEnter(theOther:Collider) {

  if(theOther.gameObject.name == "MainChar") {

    audio.PlayOneShot(CollectSound);
    yield new WaitForSeconds(1);
    Destroy(gameObject);

} }

@script RequireComponent(AudioSource)

now the problem that I am facing is that the sound just stops because the item is destroy and I don't really want to yield it for a second.

Is there any way that I can attach the sound to an empty gameobject and make that play instead when my character collides with the item ? or is there any way else to code this ?

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

6 Replies

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

Answer by duck · May 22, 2010 at 12:15 PM

This sounds like an ideal situation to use AudioSource.PlayClipAtPoint.

This function does exactly what you want automatically - it creates a new audio source at the position specified, and cleans up the audio source afterwards, making it a "fire and forget" style function.

To use it in your script, you'd change the code to something like this:

var collectSound : AudioClip

function OnTriggerEnter(theOther : Collider) {

  if(theOther.gameObject.name == "MainChar") {

    AudioSource.PlayClipAtPoint(collectSound, transform.position);
    Destroy(gameObject);

} }

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 Fariz · May 22, 2010 at 12:24 PM 0
Share

thank you so much !! thats definitely what I was looking for

avatar image mkShogun96 · Oct 31, 2016 at 10:32 AM 0
Share

Thanks man this helped me too :)

avatar image ZaKAmiar · May 01, 2020 at 08:12 AM 0
Share

Thank you very much it helped me a lot

avatar image
1

Answer by daivuk · Apr 05, 2012 at 10:55 PM

I am having the same issue, but my problem is the camera is far from the player and I tweak my AudioSource ramp for each sounds. But with this, I have no choice to use the default setting..

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
1

Answer by oktemre · Jul 29, 2015 at 01:36 PM

What I do is a bit different but works for me. I disable mesh renderer of the object and destroy in some seconds in order to let the audio effect to play till end.

 gameObject.GetComponent<MeshRenderer>().enabled = false;
 Destroy(gameObject, 5f);

Comment
Add comment · Show 1 · 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 WolfeMan · Jun 01, 2016 at 11:59 PM 0
Share

In my opinion this is a lot simpler, thank you, sir!

avatar image
0

Answer by vyonox · Jun 02, 2016 at 11:22 AM

I just had the same problem the other day and use this solution. I don't know if it is efficient but it is similar to @oktemre solution: it is possible to edit the AudioSource in the editor and keep it as part of the prefab:

  • Create and empty GameObject child of the game object you want to destroy.

  • Add the AudioSource component to the empty GameObject. Edit the audio source.

  • In your script, create a reference to the AudioSource. I called it "effect".

  • Then use this code to play the audio source:

     effect.transform.parent = null;
       effect.Play();
       Destroy(effect.gameObject, 1);
       Destroy(gameObject);
    
    

That way, the empty game object is detached from the destroyed parent and can play for 1 second and then get destroyed (or more time if you need).

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 Vandell · Oct 09, 2018 at 09:53 PM

The only drawback with PlayClipAtPoint, which is a really fine solution, is that you have 0 control over the audio source parameters, you might need to specify an audio mixer group for example.

You could implement your own PlayClipAtPoint which returns the AudioSource

 public void PlayDettaching() {
     var dettachedGo = transform.Find("Audio").gameObject;
     var audioSource = dettachedGo.GetComponent<AudioSource>();
     audioSource.Play();
     dettachedGo.transform.parent = null;                
     Destroy(dettachedGo, audioSource.clip.length);
 }

If you want to see a more detailed explanation I've blogged about this.

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
  • 1
  • 2
  • ›

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

11 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

Related Questions

java.lang.IllegalStateException: stop() called on uninitialized AudioTrack. 1 Answer

How to Stop all audio 10 Answers

Music on multiple scenes 2 Answers

How to stop Audio not all?? 0 Answers

Making sounds wait for each other to finish 2 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