Whats is wrong with this script (timer)

Hello! :slight_smile:
I learn use timer in script JS i have this code:

var timer : float = 900;
var timerMax : float = 15.0;

private var enter : boolean;

private var work : boolean;

function Start()

{

 enter = false;
 
 }

function Update()

{

work = false;
 enter = false;
   
if(work){

 timer += Time.deltaTime;
 
    if (timer >= timerMax)
    {
       Debug.Log("End!");
 
       // reset timer
       timer = 0;
    }
}


}


function OnTriggerStay(other: Collider)
{

if(other.gameObject.tag == "Player")

{

 enter = true;
 
 

if(Input.GetKey(KeyCode.G))

{

work = true;

    }
}

}

Player must click “G” when is trigger and then timer is active and when its value go down, displayed log “END”

but this concept is not work.

You are setting ‘work’ to false on line 20, so there is no way for it to be true on line 23. If I remember correctly (you’ll have to check it out), Update() gets called after OnTriggerStay(). So you may be able to fix your code by moving lines 20 and 21 to line 35. But if it were me, I’d redo the logic. That is I’d set ‘enter’ to true on OnTriggerEnter(). and set enter to false on OnTriggerExit(). Then I’d get rid of ‘work’ and move the GetKey into Update(). Replace line 23 with:

if (enter && Input.GetKey(KeyCode.G)) {
    // Timer code goes here.    
}