• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dgrohl91 · Jun 05, 2016 at 08:35 PM 0
Share

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 ();
         }
     }
 }
     
avatar image kristof1104 Dgrohl91 · Jun 05, 2016 at 08:43 PM 0
Share

try changing the grow function to:

 void grow(){
       counter++;
 }

avatar image Dgrohl91 kristof1104 · Jun 05, 2016 at 08:50 PM 0
Share

But if I do so, the counter will be increased without a limit, and I have a goal so to speak

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MissingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it. 5 Answers

how do i change max size value of text component unity? 1 Answer

Null reference exception to a non-null text object? 1 Answer

How to keep text sharp with a scaled UI ? 0 Answers

Toggle UI Text With UI Toggle Button 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges