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 pwnisher · Oct 26, 2012 at 03:21 PM · 3dobjectgame

Destroy GameObject and play sound

Hi there, i actually have this script for play song when i touch it

 var stringToEdit = " ";
 var showgui = false;
 var playClip : AudioClip;
 
 function OnGUI () {
     if (showgui) {
         // Make a multiline text area that modifies stringToEdit.
         stringToEdit = GUI.TextArea (Rect (10, 10, 300, 100), stringToEdit, 200);       
     }
 }
 
 function OnTriggerEnter(){
     showgui = false;
     audio.clip = playClip;
     audio.Play();
     }


My question is this...

if i put Destroy(gameObject); it destroys but not play the audio

the only problem is when i turn back and go in the trigger again, the sound begin again,, so i want when im over the trigger it play the sound, but the triggers destroy and the sound is still playing.

thanks a lot

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 electricsauce · Oct 26, 2012 at 04:00 PM 0
Share

are you putting the destroy gameobject after you play the audio clip?

avatar image Kiloblargh · Oct 26, 2012 at 04:29 PM 0
Share

Are you sure you need to Destroy it at all? Just setting it to inactive or disabling its components is usually faster, especially if you're going to Instantiate another one later.

1 Reply

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

Answer by Sundar · Oct 26, 2012 at 04:07 PM

You need to wait for audio to complete playing and call destroy gameobject. That is you need a coroutine to do that, like

 function OnTriggerEnter(){
     showgui = false;
     audio.clip = playClip;
     audio.Play();
     yield WaitForSeconds(2);
     Destroy( this.gameObject );
     }
Comment
Add comment · Show 4 · 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 pwnisher · Oct 26, 2012 at 04:42 PM 0
Share

Thanks you so so much dude :) !!!!!!!

avatar image pwnisher · Oct 26, 2012 at 04:45 PM 0
Share

but im having the same problem, when i walk again to the trigger the sound again replay, and i want to play it 1 time

avatar image Sundar · Oct 26, 2012 at 05:39 PM 0
Share

You have to check your logic, you have coded to play the clip OnTriggerEnter(), hence its playing and also it gets destroyed.

avatar image pwnisher · Oct 26, 2012 at 06:32 PM 0
Share

problem solved ;)

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

12 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

Related Questions

stop song and play song 0 Answers

Adding force to a bullet 3 Answers

Animation flashlight when run 4 Answers

How to do a SLiding door animation in Unity 3d with scripting. 1 Answer

Save Position script. 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