Question by
Dgrohl91 · Jun 05, 2016 at 05:59 PM ·
uitextcoroutinesfor
Making a growing number
Hi everyone
I want to make some kind of presentation with Unity, this project includes numerical statistics so I want to make them look alive with the growing "animation". My problem is that even with a Coroutine this number is shown instantly.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class grownumber : MonoBehaviour {
public int limit;
private int counter;
private Text text_n;
// Use this for initialization
void Start () {
text_n = GameObject.Find ("number").GetComponent<Text>();
counter = 0;
limit = 60000;
}
// Update is called once per frame
void Update () {
}
void grow(){
for(int i=0;i<limit;i++){
StartCoroutine ("count");
}
}
void OnTriggerEnter (Collider other){
if (other.tag == "Player") {
print ("collider");
grow ();
}
}
IEnumerator count(){
yield return new WaitForSeconds (0.5f);
counter+=1;
text_n.text = ""+counter+"";
}
}
Comment
Answer by Mmmpies · Jun 05, 2016 at 07:12 PM
How about adding the start time in start and if Time.time > Starttime + 0.5f show text.
private float StartTime;
private bool shownText =false;
void Start(){
StartTime = Time.time;
void Update(){
if(Time.time > StartTime + 0.5f && shownText == false)
{
ShowTextFunction();
shownText = true;
}
Should work and if you want to do it every .5 seconds just have that function reset StartTime and the bool to false.
I get the same result, but thanks. $$anonymous$$aybe I've misunderstood your message. $$anonymous$$y code now look like this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class subircifra : $$anonymous$$onoBehaviour {
public int limite;
private int contador;
private Text texto;
private float startTime;
private bool shownText;
// Use this for initialization
void Start () {
text_n = GameObject.Find ("50$").GetComponent<Text>();
counter = 0;
limit = 60;
startTime = Time.time;
}
// Update is called once per frame
void Update () {
if(Time.time > startTime + 0.5f && shownText == false){
text_n.text = ""+counter+"";
}
}
void grow(){
while (counter < limit) {
counter++;
}
}
void OnTriggerEnter (Collider other){
if (other.tag == "Player") {
print ("collider");
grow ();
}
}
}
try changing the grow function to:
void grow(){
counter++;
}
But if I do so, the counter will be increased without a limit, and I have a goal so to speak