Hello I have this script for a timer that counts down. I do not want it to start when the game starts I want it to start when the player passes by a trigger, Here is the script if anyone can point me in the right direction it would be greatly appreciated. Muchas Gracias.
private var initialTime : float;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var seconds : int;
private var minutes : int;
private var textTime : String;
var CountSeconds : int;
var CountMinutes : int;
function Awake() {
initialTime = Time.time;
CountSeconds = CountSeconds + (CountMinutes * 60);
}
function OnGUI () {
var guiTime = Time.time - initialTime;
restSeconds = CountSeconds - (guiTime);
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
seconds = roundedRestSeconds % 60;
minutes = roundedRestSeconds / 60;
textTime = String.Format ("{0:00}:{1:00}", minutes, seconds);
GetComponent(GUIText).text = textTime;
if(minutes <= 0 && seconds <= 0)
{
Debug.Log("Time is over");
Time.timeScale= 0;// This will pause the game
}
}
Please use the button with 0's and 1's to format your code next time. Some moderators just reject questions with unformatted code.
The timer actually starts counting when initialTime is set to the initial time. If it continuously updates to the current time, no changes will occur in the display. This can be done with minor changes in your code: keep updating initialTime inside OnGUI until a control variable is set by the trigger - like this (trigger script):
...
var CountSeconds : int;
var CountMinutes : int;
private var count = false; // this variable controls the timer
function Awake(){
CountSeconds = CountSeconds + (CountMinutes * 60);
}
function OnTriggerEnter(other: Collider){
count = true; // start the countdown
}
function OnGUI () {
if (count == false){ // if timer not counting yet
initialTime = Time.time; // update initialTime to the current time
}
var guiTime = Time.time - initialTime;
restSeconds = CountSeconds - (guiTime);
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
seconds = roundedRestSeconds % 60;
minutes = roundedRestSeconds / 60;
textTime = String.Format ("{0:00}:{1:00}", minutes, seconds);
GetComponent(GUIText).text = textTime;
if(minutes <= 0 && seconds <= 0)
{
Debug.Log("Time is over");
Time.timeScale= 0;// This will pause the game
}
}
EDITED: I’ve not noticed at first, but you’re mixing two different creatures: GUIText and the GUI system. OnGUI should be used with the GUI system, while GUIText usually is updated at Update (but can be refreshed inside OnGUI as well). In this case, the script is attached to the trigger, thus it can’t have a GUIText component because the position has different meanings for both - it’s in viewport coordinates (0…1) in a GUIText component. Since the trigger is somewhere in the 3D world, your GUIText was simply kicked out of the screen (that’s why it disappeared).
In order to reduce the changes in your code, you could just add a GUIText reference (gText variable below), create a GUIText object and drag it to the gText field in the Inspector:
...
var CountSeconds : int;
var CountMinutes : int;
// declare this GUIText field:
var gText: GUIText; // drag here the GUIText from the Hierarchy view
private var count = false; // this variable controls the timer
function Awake(){
CountSeconds = CountSeconds + (CountMinutes * 60);
}
function OnTriggerEnter(other: Collider){
count = true;
}
function Update () { // use Update instead of OnGUI
if (count == false){
initialTime = Time.time;
}
var guiTime = Time.time - initialTime;
restSeconds = CountSeconds - (guiTime);
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
seconds = roundedRestSeconds % 60;
minutes = roundedRestSeconds / 60;
textTime = String.Format ("{0:00}:{1:00}", minutes, seconds);
// update the GUIText object:
gText.text = textTime;
if(minutes <= 0 && seconds <= 0)
{
Debug.Log("Time is over");
Time.timeScale= 0;// This will pause the game
}
}
@ aldonaletto hey thanks yours worked now it starts when the trigger is hit, but now it does not show the counter on screen and i kept getting the null reference error so I removed this from the script GetComponent(GUIText).text = textTime; its what is causeing the error and maybe the reason why I can't see it on screen. Muchas gracias now I just have to figure out how to get the timer back on screen.
@Aldonaletto muchas, muchas gracias, it worked like a charm now I can continue to work on the rest of my game. I will keep looking over more reference materials so I don't keep mixing up things that just don't work together, again thank you very much now I can take a break before the next headache.
You can attach this script to a GameObject that has a collider with the trigger property set to true. If you use the function “OnTriggerEnter”, you can tell your timer script to start counting.
where would I put the "ontriggerenter" on the script?
Please use the button with 0's and 1's to format your code next time. Some moderators just reject questions with unformatted code.
– JamoraSorry will do next time.
– Kobohdashi