x


How do I Destroy() powerup and get sound to play?

Hello everyone. I have a powerup and I'm trying to get a sound to play when it's picked up, however if I destroy the powerup that the sound component is attached to it won't play. I then figured I could disable the renderer on the object until the sound plays, then delete the object and the sound, but this didn't work because although the object is invisible it can still be picked up multiple times before it gets destroyed.

My player collides with a crate, the powerup gets instantiated and a sound is played. After 2 seconds all three are destroyed. How can I do this?

var crateCollectionSnd:GameObject;
var minesCollected:GameObject;

function Update () {

}

function OnTriggerEnter(col:Collider){
    if(col.gameObject.tag == "Player"){
        GameObject.Find("PlayerPrefab").GetComponent(FireScript).Mines();
       var cloneSound = Instantiate(crateCollectionSnd, transform.position, transform.rotation);
       var cloneMinesCollected = Instantiate(minesCollected, transform.position, Quaternion.Euler(Vector3(0,180,0)));

       renderer.enabled = false;
       yield WaitForSeconds(2);
       Destroy(cloneMinesCollected);
       Destroy(cloneSound);
       Destroy(gameObject);
    }else if(col.gameObject.tag == "Bullet" || col.gameObject.tag == "EnemyBullet"){
       Destroy(gameObject);
    }else if(col.gameObject.tag == "Enemy"){
       Destroy(gameObject);
    }
}
more ▼

asked Apr 24 '12 at 03:06 PM

Hedonsoft gravatar image

Hedonsoft
54 12 25 30

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Have you tried adding a boolean called canPickup? and placing it after col.gameObject.tag == "Player"? and if it's true then set it to false. It whould look like this :

var crateCollectionSnd:GameObject;

var minesCollected:GameObject; private var canPickup:boolean = true;

function Update () {

}

function OnTriggerEnter(col:Collider) { if(col.gameObject.tag == "Player" and canPickup) { canPickup = false; GameObject.Find("PlayerPrefab").GetComponent(FireScript).Mines(); var cloneSound = Instantiate( crateCollectionSnd, transform.position, transform.rotation); var cloneMinesCollected = Instantiate(minesCollected, transform.position, Quaternion.Euler(Vector3(0,180,0)));

  renderer.enabled = false;
   yield WaitForSeconds(2);
   Destroy(cloneMinesCollected);
   Destroy(cloneSound);
   Destroy(gameObject);
}else if(col.gameObject.tag == "Bullet" || col.gameObject.tag == "EnemyBullet"){
   Destroy(gameObject);
}else if(col.gameObject.tag == "Enemy"){
   Destroy(gameObject);
}

}

more ▼

answered Apr 24 '12 at 05:16 PM

slkjdfv gravatar image

slkjdfv
330 24 28 32

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x764
x525
x130
x7

asked: Apr 24 '12 at 03:06 PM

Seen: 610 times

Last Updated: Apr 24 '12 at 05:17 PM