Setting a Time Limit on Actions

I have this script here that basically scales an object’s box collider into nothingness,

var size:Vector3;

function Update ()
{
    if (Input.GetKey ("s")) 
    {
         collider.size = Vector3(0,0,0);
    }
    else
    {
        collider.size = size;
    }
}

But I don’t want the collider to stay like this forever. So how can I add a timer to it, so that maybe after 3 seconds it reverts back to collider.size = size. Thanks for the help!

-Rov

Ok, sorry about the delay, but I got it working pretty well now. This will require a few steps. First off, here is the code.

#pragma strict
public var trans : Transform;
private var size = Vector3(1, 1, 1);
private var resize = false;
private var timer : float;

function Update() {
	if (Input.GetButtonDown("ResizeCube"))
	{
		trans.localScale = Vector3(0,0,0);
		resize = true;
		timer = Time.realtimeSinceStartup;
	}

	if(resize == true && Time.realtimeSinceStartup - timer >= 3)
	{
		resize = false;
		trans.localScale = size;
	}
}

Now, you will need to pass in the transform of the object you want resized. You can do this in the editor on this script. Then you will need to create a new axes for the button you want to press in order to initiate the resize. To do this, click on ‘Edit/Project Settings/Input’ Then expand ‘Axes’. Increase size by one, this will copy the last axes. Rename it, and set ‘Positive Button’ to whatever you’d like. Now, ensure the name of the axes matches whatever you pass into

Input.GetButtonDown()

Now I’d like to mention the two main things I used to get this to work. First is
Time.realtimeSinceStartup
This returns the real time in seconds since the game started. Basically, when the button is pressed, we set a variable to this value. Then on update, we check if

Time.realtimeSinceStartup - timer >= 3

If it is, then we know it has been at least 3 seconds.
The next thing that made this possible is

Transform.localScale

This is what I used to scale the object. This is the same scale that is in the editor under Transform whenever you select an object.

Let me know if this is what you were looking for, thanks!

var size:Vector3;
var resetTime = 0f;
var isReset = false;
function Update () {

if (Input.GetKey ("s"))

{

collider.size = Vector3(0,0,0);
isReset = true;

}

if(isReset){
if(resetTime<=3){
resetTime+=Time.deltaTime;
}
if(resetTime>3){
collider.size = size;
isReset = false;
resetTime = 0;
}
}

}