if () time start

I want to start to run a timer value only when the if condition is true.
My problem is that Tim.tim is the value since game started. Somehow that value should set to zero to make the script work

if (onPlate == true)
{
timeLeft =   Time.time;
if (timeLeft > 15) 
{
	//end game
}
}

Rather write : timeLeft = timeLeft + time.deltaTime;

agree but that doesn’t set my time back to zero.
I am looking for way when my FP controller stands on cube the timer start to run starting from 0 and when the FP leave the cube it is back to zero, when the FP goes back on it, the timer starts again to count up starting from zero.

With my current script: when the FP goes on the plate the value of the time is right away the same amount of the Time.time since the game started.

if (onPlate == true)

{
timeLeft = startTime  + Time.time; 

if (timeLeft > 15) 
{

	timeLeft = 0;
    startTime = 0;
	Application.LoadLevel("gameover");
	
}
}

else
{
timeLeft = 0;
startTime = 0;
}

}

On entering the plate

startTime = Time.time;

While Standing on the plate

elapsedTime = Time.time - startTime; 
if (elapsedTime > 15) 
{
    Application.LoadLevel("gameover");
}

On leaving plate

elapsedTime = 0;

The elapsed time is the difference between the start time and the current time.

Where these blocks of code end up is very dependant on your code.

for while standing on the plate is this function correct?

function OnControllerColliderHit(hit:ControllerColliderHit){
			
	if(hit.gameObject.tag == "targetplace"){
		//startTime = Time.time; 
		onPlate = true;
		print ("yes");
        elapsedTime = Time.time - startTime;

        if (elapsedTime  > 15) 
          {
	Application.LoadLevel("gameover");
	
          }
           
         else{
         elapsedTime = 0;	
         }

Any advise I should use for “On entering the plate”

thanks for the help

c

This is what I apply in my text as a timer.

this is wat i apply in my character.

( i wanted the timer to start only when it hit the concrete floor. But it still starts although i am not on the concrete floor.) Can help?

Get rid of the “timeActive” on your timer script and instead have it reference Character.timeActive (only replace “Character” with whatever your character script name is)

(I NAME THIS SCRIPT AS timerStart applied to my 3rd person character tag as Player)

u mean like that? But i have error saying expressions in statements must only be executed for their side-effects.

I think this is throwing the error :

timerStart.timeActive;

Shouldnt it be :

timerStart.timeActive = false;   // true

–
oxl

Not quite, I meant to change your references to “timeActive” to point instead to “timerStart.timeActive”, like this:

function Start()
{
	startTime = 300;
	timerStart.timeActive = false;
}

.........

if (timerStart.timerActive == true)
{
	startTime -=1 * Time.deltaTime;
	guiText.text = FormatTime(startTime);
}

There is no error.
But the timer still start count down although my character haven hit on concrete floor.
Any other idea I can fix it?

Maybe put the timeActive check at the beginning of your update function?

FizixMan, the script worked when trigger on floor the timer only start…
But I have other problem occur when timer<0.1 then application load to game over scene…
when i wanted to restart the level… my timer does not reset the time…
Do you why?

this is my script

Maybe set timeActive back to false?

if (timeLeft < 1.0) {
timerFPS.timeActive = false;
Application.LoadLevel (loadsceneGameOvr);
}

nope its not working… it does not reset to the startTime
I have another script that can reset time… i tried adjusting it to fit into script… dint work…

Here is the script i have found that will reset the timer…

ANy idea how to fix it into my previous script?

Oh, if you want to reset the time, maybe you can just reset the variables:

if (timeLeft < 1.0) {
timerFPS.timeActive = false;
startTime = 60.0;
minutes = 0;
seconds = 0;
fraction= 0;
timeLeft = startTime;
Application.LoadLevel (loadsceneGameOvr);
}