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 miketyler1 · Dec 17, 2013 at 01:38 AM · gameobjectaudiodestroytimerdelay

Delay "Destroy(gameObject)"

I have a cube which I want to function in the following manner: when the player collides with the cube, a sound plays, and when the sound finishes (three seconds later), the cube s destroyed. Now how exactly do I do this?

Comment
Add comment · Show 1
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 Josh707 · Dec 17, 2013 at 01:45 AM 0
Share

If you want it to be precisely when the audio is finished, make sure the audio source isn't set to loop and use audio.IsPlaying() to deter$$anonymous$$e if it's not playing anymore.

Another way, if you know the exact length of the clip, is to use Destroy(gameObject, someTime);

You might even be able to just use Destroy(gameObject, audioclip.length); but I remember reading somewhere that some compression settings makes the time not accurate.

2 Replies

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

Answer by YoungDeveloper · Dec 17, 2013 at 01:57 AM

You do this using WaitForSeconds().

http://docs.unity3d.com/Documentation/ScriptReference/WaitForSeconds.html

It's easier in JavaScript (Unity script), but for C# you need to create special separate function for it.

 //When you want to destroy it, call the function
 StartCoroutine(Die());
 
 
 //And function itself
 IEnumerator Die(){
     //play your sound
     yield return new WaitForSeconds(3); //waits 3 seconds
     Destroy(gameObject); //this will work after 3 seconds.
 }
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 The9thMan99 · Mar 20, 2015 at 12:23 PM 0
Share

Worked perfectly for me, thanks!

avatar image
13

Answer by Genei_180 · Oct 25, 2016 at 09:24 PM

I know this is a little bit older but:

Destroy has a second argument where you can make it wait for seconds

Example:

 // Kills the game object in 5 seconds after loading the object
 Destroy (gameObject, 5);


Here is the link for the Api: API Destroy

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

20 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Why is Destroy(this.gameObject) not working immediately? 2 Answers

How to delay lines of code. 2 Answers

Destroy object when transform.position.x > 10. 1 Answer

Destroy(gameObject) does not work 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