Getting the Timer to Hold Count Correctly

Okay I suspect I am just missing a variable or misplacing some part of these two codes. The scenario is I have a timer that is counting down. When I hit an object I want to add some more time to the timer. BUT with the code I have now, the currentTime will update very briefly, then revert back to the countdown time. I know the currentTime is stored correctly but I think the UpdateTimerText function is confused and I just can’t see what to do. Thanks for the help. You guys are awesome.

Script A - Timer.js

// the textfield to update the time to
private var textfield:GUIText;

// time variables
public var allowedTime:int = 90;
public var currentTime;

function Awake()
{

    currentTime = allowedTime;

    // retrieve the GUIText Component and set the text
    textfield = GetComponent(GUIText);

    UpdateTimerText();

    // start the timer ticking
    Tick();
}

function Tick()
{
    // while there are seconds left
    while(currentTime > 0)
    {
       // wait for 1 second
       yield WaitForSeconds(1);

       // reduce the time
       currentTime--;

       UpdateTimerText();
    }

}

function UpdateTimerText()
{
    // update the textfield
    textfield.text = currentTime.ToString();
}

Script B - ObjectInteraction.js

`private var textfield:GUIText;
public var currentScore: ScoringScript;

function OnCollisionEnter(collision : Collision) {    
    if (collision.gameObject.name == "Capsule"){
    currentTime.currentTime += 10;
    currentTime.UpdateTimerText();
     
  			 } 			
 }

From Scribe- same problem still persists even with revised code of not “holding” the new currentTime after object Interaction past one frame

There are a few errors in Script A, I’ve rewritten your code slightly, hopefully this will work…

Script A - Timer.js

var textfield : GUIText; //don't set this to private else line 6 of ObjectInteractions will throw an error

public var allowedTime : int = 10000;
public var currentTime : int;
    
function Start(){
    textfield = GameObject.Find("GUI Score/txt-score").GetComponent(GUIText);
    currentTime = allowedTime;
    textfield.text = allowedTime.ToString();
    InvokeRepeating("Tick",1,1);
}

function Tick(){
    currentTime-=1;
    textfield.text = currentTime.ToString();
}

Script B - ObjectInteraction.js

public var currentTime: Timer;

function OnCollisionEnter(collision : Collision) {    
    if (collision.gameObject.name == "Capsule"){
       currentTime.currentTime += 10;
       currentTime.textfield.text = currentTime.currentTime.ToString();
    }          
}

Hope that solves your problem,

Scribe

Here are some pictures of the problem for further clarity. After 10 seconds Timer looks like this

4677-9990.png

When I hit the object timer looks like this for 1 frame (which is correct):

4678-9980.png

But then after the 1 frame time timer reverts back to where it was initially 9990 less 1 so 9989.

What it should obviously do is countdown now from the 9980 to 9979.

currentTime.UpdateTimeText();

this calls the script, that makes it awake that resets the time because of this

currentTime = allowedTime;

change
function Awake()

to

function Start()

now it will only set the value once the very first time its ever called. not each call

happy coding