Destroying a Block.

Hello.
I am making this game and i can’t get part of it working.

I have this script what destorys itself after 1 second

Destroy (gameObject, 1);

Then if you press x (what i called undestroy) it stops the destory

if(Input.GetButtonDown("undestroy"))
{

// WHAT SCRIPT DO I NEED TO PUT HERE TO CANCEL THE 

}

Ok thanks

-Jamesterjim

Instead of initiating the timed destroy call, it would make more sense to run a [1 second] timer, and destroy the object if the appropriate key hasn’t been pressed.

Use Invoke and CancelInvoke.

^^ This would deffinitely be the better idea and implementing a simple timer is extremely easy

private var timer : float = 0;
var triggerTime : float = 1;

function Update () {
   ...
   timer += Time.delta;

   if (timer >= triggerTime) {
      timer = 0;
      ...
   }
   ...
}

This is of course just basic but you can see how easily you can adjust it to fit your needs.

For example you could have a variable that woudl return true when the objects destruction countdown begins and then if the undestroy button is pressed during the countdown just cancel the countdown. Then you can add the result of the countdown being if timer reaches 1 second then destroy the object.

Hope that helps