- Home /
TextMesh counter in real-time
I'm creating a game where a number needs to count in real-time. I'm struggling to find the variables to get this working. Here's what I currently have:
#pragma strict
var myNumber : TextMesh;
var myHealth : int;
var numberUpdateRate : float;
function Start ()
{
var myHealth = 0; // Brain 'Influence Point' value
}
function Update ()
{
UpdateNumber();
}
function UpdateNumber()
{
if(Time.frameCount %numberUpdateRate == 0)
{
myHealth ++;
myNumber.text = myHealth.ToString();
}
}
@iconiic, as Eric says $$anonymous$$ETE your "update' function entirely. Put the following line in Start()
InvokeRepeating("increaseScore", 1.0 , 1.0 )
then ...
function increaseScore() { myHealth++ whatever; }
I encourage you to try to learn what "Update()" does. Some beginners think you should use it all the time because you often see it in examples. Basically don't use it until you really know what it does eh! You can read 100s of questions about it on here so no need to repeat, good luck!
You're creating a new "myHealth" variable in Start, which is not related to the "myHealth" variable already created above, and then not doing anything with it.
Thanks Fattie, it's working great. However, it counts much more quickly than seconds:
pragma strict
var myNumber : Text$$anonymous$$esh; var myHealth : int; var numberUpdateRate : float;
function Start () { InvokeRepeating("IncreaseScore", 1.0 , 1.0 ); }
function Update () { IncreaseScore(); }
function IncreaseScore() { myHealth++; myNumber.text = myHealth.ToString(); }