Quick question about destroying bullets/objects

I have this script to shoot a cube, I have it set up so upon hitting the player. Known as wall, it waits one second then destroys itself. What I want to know how to do is edit this code so if it doesn’t collide with the player, or collides with anything else it waits 5 seconds then destroys itself, here’s what I have:

Bulletcollision.js

var damage = 5;

function OnCollisionEnter(theCollision : Collision){

 if(theCollision.gameObject.name == "Wall"){

//take 5 points from health of the wall
Player.health -= damage;
yield WaitForSeconds(1.0);
Destroy(gameObject);

}
else 
yield WaitForSeconds(5.0);
Destroy(gameObject);
}

Thanks ahead of time guys!

Well, I'm confused how to make it so that even if it doesn't collide have it destroy after 5 seconds.

1 Answer

1

Ah, thats an easy one,

function Start()
{
   Destory(gameobject, 5);
}

the destroy function has a second parameter you can put in that takes in a float which is in seconds. So when the object starts up, it queues the bullet to get destroyed in 5 seconds, you can also replace the yield with the way i did the destroy function.

Excellent. Yes, do come back. Always good to hear about improved design. This is a site for Answers, after all... Full scale Object Pooling involves create an array of entities at the start of the game, and only enabling them (gameObject.SetActive(true)) as required. When one dies, it gets disabled. Still that's for another day.