- Home /
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
are you putting the destroy gameobject after you play the audio clip?
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.
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 );
}
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
You have to check your logic, you have coded to play the clip OnTriggerEnter(), hence its playing and also it gets destroyed.
Your answer
Follow this Question
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