Cant get my Timer to add time.
var startTime:float;
var timeRemaining:float;
var count:int = 0;
var started:boolean = true;
function Start()
{
}
function Update ()
{
Countdown();
}
function Countdown()
{
if(started == true && count == 0)
{
startTime = 60.0;
count++;
}
timeRemaining = startTime - Time.time;
ShowTime();
if(timeRemaining < 0)
{
timeRemaining = 0;
TimeIsUp();
}
}
function ShowTime()
{
var minutes:int;
var seconds:int;
var timeString:String;
minutes = timeRemaining/60;
seconds = timeRemaining%60;
timeString = minutes.ToString() + ":" + seconds.ToString("D2");
guiText.text = timeString;
}
function TimeIsUp()
{
Application.LoadLevel("Lose Screen");
}
//script that tells timer to add time
var points:int;
var addTime: Timer;
function Start ()
{
addTime = GameObject.Find("Timer").GetComponent(Timer);
points = 0;
}
function Update ()
{
}
function OnTriggerEnter(Col : Collider )
{
if(Col.gameObject.tag == "Pickup")
{
points += 10;
Destroy(Col.collider.gameObject);
}
else if(Col.gameObject.tag == "Token")
{
addTime.timeRemaining += 5.0;
Destroy(Col.collider.gameObject);
}
}
Answer by NoseKills · Jan 17, 2016 at 11:29 PM
You are calculating timeRemaining
in Update()
timeRemaining = startTime - Time.time;
ShowTime();
if(timeRemaining < 0) {
...
It doesn't matter if you add to timeRemaining in some other script since your main script is setting the value of it in a way that overrides everything else :
// other script
timeRemaining = 1000000000;
// main script
timeRemaining = startTime - Time.time;
You have to do one of the following (for example)
1) add another variable to keep track of added time and use that in calculating timeRemaining timeRemaining = startTime - Time.time + addedTimes;
2) add your += 5.0f
to startTime
instead
3) count down timeRemaining with -= Time.deltaTime
so you don't completely override it at any time
Answer by Icarus370 · Jan 18, 2016 at 12:05 AM
Ahhh that makes sense. This solved my problem. thank you!
Your answer
Follow this Question
Related Questions
Getting a timer to go to 0 1 Answer
Capture Zone Timer 0 Answers
System Timer main thread 0 Answers
Countdown timer... 2 Answers
How to make GameOve Appear when times'up in unity? 0 Answers