- Home /
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!
whats is the problem you currently have?, one thing i already see is that u have to possibility of calling yield multiple times because you are still running the collision check after you've collided with sumthing already, what you can do is have a bool like hasCollided, and when you collide with sumthing set it to true so it doesnt run the if and else again in your onCollisionEnter
Well, I'm confused how to make it so that even if it doesn't collide have it destroy after 5 seconds.
Answer by illwunn · Aug 15, 2011 at 11:11 PM
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.
Your answer
Follow this Question
Related Questions
destroy touch the screen on gameobject in camera 0 Answers
Tank Missle Colide then explode and destroy 1 Answer
"Destroy" bullet without turning off trail renderer 2 Answers
Collision Detection for a Prefab? 3 Answers
How can I delay the cast of a fireball as well as destroy a gameobject mid script 2 Answers