I am trying to make my timer start when i press the ‘G’ but the problem i get is when i press the key the timer goes up a number then freezes and i have to keep pressing the key for it to increase :S Here is my code to help out
I think getting the Input of a button checks whether it is simply down or not. When you release it, the function is no longer true and therefore the function will not be running. Try to set a bool when the key is pressed instead:
var target: Transform;
var startTime;
var timer1: int;
var timingOn: bool;
function Update()
{
if(Input.GetButton("StartWave"))
{
timingOn = true;
startTime = Time.time; //time starter
}
if (timingOn)
{
TimerStart();
}
}
function OnGUI ()
{
GUI.Label (Rect (400, 40, 100, 20), "Press G To Start The Horde");
}
function TimerStart()
{
timer1 = Time.time; //Set time
if(timer1 - startTime >= 10)
{
Debug.Log ("Timer finished, spawn now");
GetComponent(NavMeshAgent).destination = target.position;
target = GameObject.FindWithTag("Chest").Transform;
}
}
However there’s a few discrepancies with your code. For example, you use a function to start the timer and set some variables, but when this function is run again it will restart the time. You need to be using your timer1 - startTime and comparing against this to see how much time has passed.