- Home /
Making a Timer Out out of 3D Text using C#.
Hey! The title practically says it all. But I should offer a bit of detail.
I am setting up a timer that is displayed in 3D text, and most of the tidbits I have obtained in regards to timers have limited to having the numbers displayed in GUI. So what alterations should I make in order for a timer script to work for 3D text?
Two digits are enough. I need 90 seconds on the clock.
the first 30 seconds will be dedicated to demonstrating the gameplay. Then the real game will run for the next 60 seconds until it's over.
the timer should go off immediately when the scene runs.
Beyond the 3D text displaying the timer, there can be some other 3D texts displaying like booleans, like the word 'START' when the 30 seconds pass and 'stop' when the last 60 seconds are up.
Thank you all for your patience and your passion with the software!
I should have mentioned in my description that my $$anonymous$$m wants me to have this written in c# format, not in javascript.
Answer by robertbu · Dec 16, 2013 at 05:41 AM
This reads like a recipe for someone to write a script for you. Here is a basic setup you can extend with your other features.
Do: Game Object > Create Other > 3D Text
Size and position the text in the window
Add the following script to the game object (labeled 'New Text' in the Hierarchy window):
pragma strict
var time = 90.0; private var tm : TextMesh;
function Start () { tm = GetComponent(TextMesh); }
function Update () { time -= Time.deltaTime; tm.text = Mathf.RoundToInt(time).ToString(); }
Also if you want your time formatted as something like 5:32
int $$anonymous$$utes, seconds;
$$anonymous$$utes = time/60;
seconds = time%60;
if(seconds < 10)
tm.text =""+$$anonymous$$utes+":0"+seconds;
else
tm.text =""+$$anonymous$$utes+":"+seconds;
$$anonymous$$nowing just a few basics will allow you to mostly if not completely translate a high percentage of scripts that come across this list. Here is a reference link:
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
And here is the C# translation of the script above:
using UnityEngine;
using System.Collections;
public class Timer : $$anonymous$$onoBehaviour {
public float time = 90.0f;
Text$$anonymous$$esh tm;
void Start () {
tm = GetComponent<Text$$anonymous$$esh>();
}
void Update () {
time -= Time.deltaTime;
tm.text = $$anonymous$$athf.RoundToInt(time).ToString();
}
}