- Home /
How do I create a variable from a... GUIlabel?
This seems like a stupid question, but i've got a script that counts upwards from 0.0 indefinitely until the player reaches the end of the level, and I want to set the end number as a variable to be added with the numbers of all the other levels once the player has completed all those levels. Is there a simple way to do that?
Here is the script:
var MyFont : Font;
static var Timer : float = 0.0;
static var maybe : boolean = false;
static var gui : boolean = false;
function Update() {
if(maybe == true)
Timer += Time.deltaTime;
}
function OnGUI() {
if(gui == true)
{
GUI.skin.font = MyFont;
GUI.color = Color.blue;
GUI.Label(Rect(235, 420, 200, 100), "Final Time: " + Timer.ToString("0.00"));
}
else
{
GUI.skin.font = MyFont;
GUI.color = Color.blue;
GUI.Label(Rect(292, 420, 100, 100), "" + Timer.ToString("0.00"));
}
}
I need the "Timer" variable to be another variable once it is set to whatever time the player took to complete the level. Any help would be appreciated.
Timer is a variable, it's a float. Why can't you just assign that to another variable when the level is complete
sumFloat = sumFloat + Timer;
before you reset it for the next level (presumambly)
Answer by iamvishnusankar · Apr 26, 2014 at 04:47 AM
For creating timers, Coroutines are the best method.
Call the timer using
StartCoroutine ("MyTimer");
Use the following code for timer :)
public GUIText myText;
IEnumerator MyTimer()
{
//timer maximum, here's a timer counts down 30 sec
int x = 30;
for(;;)
{
myText.text = x.ToString();
//waiting for 1 second
yield return new WaitForSeconds(1f);
x-=1;
if(x==0)
{
//To do when the timer finishes couting
//Reset x=30 if you want to loop the timer
}
}
}
Your answer
Follow this Question
Related Questions
Need help with GUI label not displaying variable 1 Answer
accessing vars of other script doesn't work 1 Answer
Need to use 2 different language scripts. 1 Answer
Basic C# Public Variable Help 3 Answers
Static Variable Problem 1 Answer