A Timer Class

I’m attempting to make a timer class that parents its behavior to its derivatives. This behavior is counting down to zero when time is greater than zero.

Unfortunately, its not working, more specifically a timer derived from this timer class does not count down to zero when I run the scene. Why is this?

Below is the code used:

//Timers.
public class Timer
{
	public var time: float;
	function Update ()
	{
		if (time > 0)
		{
			time -= Time.deltaTime;
		}
		else
		{
			time = 0;
		}
	}	
	public function Timer(tm : float)
	{
	time = tm;
	}
}
public var testTimer : Timer =  new Timer(5);

You need your class to derive from MonoBehaviour if you want functions like Update to run, and it needs to be attached to some object in the scene as a component.

So you can get rid of the class definition, since in Unityscript all scripts are automatically classes that derive from MonoBehaviour anyway. You can’t use “new” when doing this, so use AddComponent instead.

var timer = AddComponent(Timer);