- Home /
Help with timer guitext
Hey im using two scripts, startRace & endRace, which starts at 60 seconds counting down when you trigger it and stops when you hit the other trigger. Can i just add some coding into these codes which make it a guitext and displays on the screen. Thank you.
startRace :
using UnityEngine;
using System.Collections;
public class startRace : MonoBehaviour {
public bool raceStarted = false;
public float raceTimer = 60.0f;
public bool raceOver = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Starts timer when race starts
if (raceStarted)
{
raceTimer -= Time.deltaTime;
}
if (raceTimer <= 0.0f)
{
raceOver = true;
raceStarted = false;
Application.LoadLevel(0);
}
}
void OnTriggerEnter(Collider raceTrig)
{
//Starts race
if(raceTrig.gameObject.tag == "Player")
{
raceStarted = true;
}
}
}
endRace:
using UnityEngine;
using System.Collections;
public class endRace : MonoBehaviour {
private startRace race;
// Use this for initialization
void Start () {
race = FindObjectOfType(typeof(startRace)) as startRace;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider raceTrig)
{
//Stops timer and ends race
if(raceTrig.gameObject.tag == "Player")
{
race.raceStarted = false;
}
}
}
sorry i will next time, do you have any idea about the new question i asked below, thanks :)
Answer by Tim-Michels · Jul 04, 2012 at 06:52 AM
Well here's a basic display of text in OnGUI:
void OnGUI()
{
GUI.Label(new Rect(x, y, width, height), raceTimer.ToString());
}
You should alter the Rect to position it to where you want. You should also take a look at
http://docs.unity3d.com/Documentation/ScriptReference/GUIStyle.html
to understand how to alter the style, size, etc. of your text.
This should help you on your way ;) cheers
well to add to this answer, here is a function that would work even if the time is more than 60 seconds!!! and display it in standard clock format.
public string GetDisplayTime(float time)
{
int $$anonymous$$ute = (int) $$anonymous$$athf.Abs(time/60);
int seconds = ((int) time)%60;
return $$anonymous$$ute.ToString("00")+":"+seconds.ToString("00");
}
void OnGUI()
{
GUI.Label(new Rect(x, y, width, height), GetDisplayTime((float)raceTimer));
}
The first code is easier for me and it works in game perfectly and just what i want it to do, but thank you anyway. But now i need to know what i have to add to either of those scripts so that once it hits that last trigger and the timer stops. I would like the game to freeze once the end trigger has been set, and for a new guitext to appear in the middle of the screen saying "You Win!"
I want to add a guitext, to this script so that when my player hits the collider, it will freeze the game and text will appear saying "you win" for 5 seconds then go back to my main menu. The player is already hitting the collider and the timer is stopping but the player is still able to move.
using UnityEngine; using System.Collections;
public class endRace : $$anonymous$$onoBehaviour {
private startRace race;
// Use this for initialization void Start () {
race = FindObjectOfType(typeof(startRace)) as startRace;
}
// Update is called once per frame void Update () {
}
void OnTriggerEnter(Collider raceTrig)
{
//Stops timer and ends race if(raceTrig.gameObject.tag == "Player") { race.raceStarted = false; }
}
}
Answer by MatthewSmoothy · Jul 04, 2012 at 12:03 PM
I want to add a guitext, to this script so that when my player hits the collider, it will freeze the game and text will appear saying "you win" for 5 seconds then go back to my main menu. The player is already hitting the collider and the timer is stopping but the player is still able to move.
Do not post comments or edits to your initial question in the answer section! Use this space for actual answers to the original question and nothing else. Use the [add new comment] button that is underneath each answer or question.