- Home /
How do I get remaining time to display on screen?
Hi guys, I've never done a timer in Unity before and I was wondering if anyone could help me out with getting time to display on the screen?
This is the script I'm working with:
var seconds = 60; private var textMesh : TextMesh;
function Start () { textMesh = GameObject.Find ("Timer").GetComponent(TextMesh); textMesh.text = seconds.ToString(); InvokeRepeating ("Countdown", 1.0, 1.0); }
function Countdown () { if (--seconds == 0) CancelInvoke ("Countdown"); textMesh.text = seconds.ToString(); }
I've applied the script to an empty game object and I added a text mesh to the object, but I don't know how to display the time on-screen so the player knows how much time they have left.
Also I'm guessing it's a very simple case of telling the timer to load a different level when it gets to 0? (I'd like it to go to a Game Over screen!)
Thanks in advance guys :).
Answer by Justin Warner · Apr 26, 2011 at 01:32 PM
Make a game object in the scene called Timer, and have a Text Mesh on the game object, it should work then...
To load a new level: http://unity3d.com/support/documentation/ScriptReference/Application.LoadLevel.html
Hope this makes sense.
Thanks for the reply. I shall try that and see if it works. Thankyou :)
Unfortunatley I'm still having the same problem, nothing is appearing on the screen at all.
Didn't think it'd be this much hassle to get the timer to appear on the screen :/
I found a work around. I changed it from Text$$anonymous$$esh to GUIText and that worked fine :)
Lol, that's good. Sorry, I couldn't think of anything =).
Your answer
Follow this Question
Related Questions
Lerpz Escapes question 1 Answer
hide gui.label after an event 1 Answer
3Dplatformer tutorial GUI script - adding a timer GUI 0 Answers
timer problem 1 Answer
Timer issue - bool value not setting timer to false? 1 Answer