- Home /
Destroy Object Over Time
I'm totally new to scripting...
I have a small working script written that destroys a game object (barrel) when in a collision with another object (bomb). I have it timed at 3 seconds. What I want to do is check that the objects are still in collision before the destroy object occurs. If not in collision object doesn't destroy.
Here is my code for the barrel. Thanks for any help.
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.name == "Extinguisher")
{
Invoke("PutOut",3);
}
}
function PutOut()
{
Destroy (gameObject);
}
Answer by robertbu · Mar 13, 2013 at 10:32 PM
You can set a flag (untested):
var colliding : boolean;
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.name == "Extinguisher")
{
colliding = true;
Invoke("PutOut",3);
}
}
function OnCollisionExit(theCollision : Collision)
{
colliding = false;
}
function PutOut()
{
if (colliding)
Destroy (gameObject);
}
It may not be a problem for your game, but if the object left and then recollided, this would still fire. If you absolutely need 3 seconds of continuous contact, create your own timer and reset it at each OnCollisionEnger().
Perfect works like a charm. Thanks for the help. So nice of you to help out the new guy.