Counter?

Is there a way to have a variable that goes down by 1 every second. Like lets say I have…

[color=blue][b]static var[/b][/color] timecount = 20;

Can I write a script where this goes down by 1 every second?
(I wrote the variable as static for a reason so if you provide scripts keep it that way thanks).

static var timecount = 20;

function Start () {
    InvokeRepeating("Counter", 1, 1);
}

function Counter () {
    timecount--;
    if (timecount <= 0) {
        CancelInvoke("Counter");
    }
}

Adjust it to fit your needs.

Thanks, this worked perfectly for me.

Is it possible to make a counter, which starts after a button is pressed and then make it count using Time.time.
I dont want my time to start from programstart but from button press. And it needs to be reliable :slight_smile:
Anyone?

Krodil, basically what I did was use the counter that was constant like above, but when a button was pressed it set it to 20. So that means that form that button press (actually power up was hit), it will last 20 seconds.

In the following scripts are how I implemented it…

//Funtion Update Variables
	var speed : float = 6.0;
	var jumpSpeed : float = 8.0;
	var gravity : float = 20.0;
	private var moveDirection : Vector3 = Vector3.zero;
	var sparkfab : Transform;
	//Meshes for character
	var MeshControl = 0;
	var Mesh1 : Texture;
	var Mesh2 : Texture;
	var Mesh3 : Texture;
	var Mesh4 : Texture;
	var Mesh5 : Texture;
	var Mesh6 : Texture;
//------------------
	static var powerjump = false;
	static var powerfly = false;
	static var powershield = false;
	//Shield Effects
	var ShieldEffect : Transform;
//------------------
	static var dead = false;
	static var timecount = 20;

function Start ()
{
	InvokeRepeating("Counter", 1, 1);
}

function Counter ()
{
	timecount--;
}

function Update()
{
//Continually time down the Powerup Time limit
//--
    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) 
	{
	// We are grounded, so recalculate
	// move direction directly from axes
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 1);
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
        
		if (Input.GetButton ("Jump")) 
		{
				moveDirection.y = jumpSpeed;
				var spark = Instantiate(sparkfab, transform.Find("SpawnPoint").transform.position, Quaternion.identity);
		}
    }
//Powerup that allows you to move while in the air
if(powerjump)
{
	if(!controller.isGrounded)
	{
		if(Input.GetKeyDown("d"))
		{
			moveDirection.x = speed;
		}
		if(Input.GetKeyDown("a"))
		{
			moveDirection.x = -speed;
		}
		if(Input.GetKeyDown(KeyCode.RightArrow))
		{
			moveDirection.x = speed;
		}
		if(Input.GetKeyDown(KeyCode.LeftArrow))
		{
			moveDirection.x = -speed;
		}
	}
[b]if(timecount <= 0)[/b]
{
	powerjump = false;
	PowerUpGUI.powerupTF = false;
}
}
//Low Gravity Power up (lets just call it fly)
if(powerfly)
{
gravity= 5;
if(!controller.isGrounded)
	{
		if(Input.GetKeyDown("d"))
		{
			moveDirection.x = speed;
		}
		if(Input.GetKeyDown("a"))
		{
			moveDirection.x = -speed;
		}
		if(Input.GetKeyDown(KeyCode.RightArrow))
		{
			moveDirection.x = speed;
		}
		if(Input.GetKeyDown(KeyCode.LeftArrow))
		{
			moveDirection.x = -speed;
		}
	}
if(timecount <=0)
{
	powerfly = false;
	PowerUpGUI.powerupTF = false;
}
}
if(!powerfly)
{
gravity = 20;
}
//Shield  recharger!
if(powershield)
{
	ShieldEffect.renderer.enabled=true;
	CharacterParticle.partcharge = true;
		if(MeshControl <= 5)
		{
			MeshControl = 0;
		}
		if(GUIH.Health < 6)
		{
			GUIH.Health += 1;
		}
	if(timecount <= 0)
	{
	powershield = false;
	PowerUpGUI.powerupTF = false;
	CharacterParticle.partcharge = false;
	}
}
if(!powershield)
{
ShieldEffect.renderer.enabled=false;
}
//Oh no you lost all your Health!
if(dead)
{
	moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
// Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
    controller.Move(moveDirection * Time.deltaTime);
//Meshes Changers
	switch(MeshControl)
	{
		case 0:
		renderer.material.mainTexture = Mesh1;
		break;
		case 1:
		renderer.material.mainTexture = Mesh2;
		break;
		case 2:
		renderer.material.mainTexture = Mesh3;
		break;
		case 3:
		renderer.material.mainTexture = Mesh4;
		break;
		case 4:
		renderer.material.mainTexture = Mesh5;
		break;
		case 5:
		renderer.material.mainTexture = Mesh6;
		dead = true;
		break;
	}
}

function OnTriggerEnter( hit : Collider )
{
	if(hit.gameObject.tag == "Crate")
	{
		MeshControl += 1;
	}
}

And this activates a power up…

var SpinSpeed = 5;
var sparkfab : Transform;

function Update () 
{
var spin = SpinSpeed * Time.deltaTime;
transform.Rotate(spin, spin,0);
}
function OnTriggerEnter( hit : Collider )
{
	if(hit.gameObject.tag == "Player")
	{
		Character.timecount = 20;
		PowerUpGUI.powerupTF = true;
		var LifeTime = 1 * Time.deltaTime;
		Character.powerjump = true;
		var spark = Instantiate(sparkfab, transform.Find("SpawnPoint").transform.position, Quaternion.identity);
		Destroy (gameObject, LifeTime);
	}
}

So every time I hit the collider of a power up (or in your case you can just make a button) it will set the counter to 20 and count down. Once the counter finishes the power up will become false.