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);
}
}