Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make GUIText change over time?

Discussion in 'Scripting' started by Treasureman, Dec 3, 2014.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Hello
    I have a GUIText and I want it to say 1%, then 2%, then 3%, etc. Every 1.2 seconds until it hits 100%. How would I do this?
     
  2. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    This is just a small example to give you an idea:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. namespace rkk.unity.experimental
    6. {
    7.  
    8.    private int onePointTwoSecCounter = 0;
    9.    private float optscNextUpdate = 1.2f;
    10.    private float optscLastUpdate = 0.0f;
    11.  
    12.     public class rkkExperimental1 : MonoBehaviour
    13.     {
    14.  
    15.  
    16.         void Update ()
    17.         {
    18.  
    19.             optscLastUpdate = optscLastUpdate + (Time.deltaTime * Time.timeScale);
    20.             if (optscLastUpdate >= optscNextUpdate)
    21.             {
    22.                 onePointTwoSecCounter++;
    23.                 optscNextUpdate = optscNextUpdate + 1.2f;
    24.                 if (onePointTwoSecCounter >= 100)
    25.                 {
    26.                     optscNextUpdate = optscNextUpdate + 10000000000.0f;
    27.                 }
    28.             }
    29.  
    30.             UpdateGUIText();
    31.  
    32.         } //<- void Update ()
    33.  
    34.  
    35.        void OnGUI()
    36.           {;
    37.              UpdateGUIText();
    38.           } //<- void OnGUI ()
    39.        
    40.        
    41.        
    42.           void UpdateGUIText()
    43.           {
    44.               guiText.text = string.Format("{0}%", onePointTwoSecCounter);
    45.           } //<- void UpdateGUIText()
    46.  
    47.  
    48.     }
    49.  
    50. }
    51.  
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I'm sorry but I don't know C# very well. Is there any way you could give some sort of link or something so I could see it in jacascript? Maybe in the unity docs
     
  4. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    I'm not that much of a JS guy, but it should looks like:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. // Ignore the 2 namespace warnings: http://forum.unity3d.com/threads/how-can-i-do-warning-message-namespace-system-collections-is-never-used-bcw00.103287/
    4.  
    5. private var onePointTwoSecCounter : int = 0;
    6. private var optscNextUpdate : float = 1.2;
    7. private var optscLastUpdate : float = 0.0;
    8.  
    9. function Start ()
    10. {
    11.  
    12. }
    13.  
    14. function Update ()
    15. {
    16.  
    17.    optscLastUpdate = optscLastUpdate + (Time.deltaTime * Time.timeScale);
    18.    if (optscLastUpdate >= optscNextUpdate)
    19.    {
    20.        onePointTwoSecCounter++;
    21.        optscNextUpdate = optscNextUpdate + 1.2;
    22.        if (onePointTwoSecCounter >= 100)
    23.        {
    24.            optscNextUpdate = optscNextUpdate + 10000000000.0f;
    25.        }
    26.    }
    27.  
    28.    UpdateGUIText();
    29.  
    30.  
    31. }
    32.  
    33. function OnGUI()
    34. {
    35.    UpdateGUIText();
    36. } //<- void OnGUI ()
    37.  
    38.  
    39. function UpdateGUIText()
    40. {
    41.    guiText.text = onePointTwoSecCounter + "%";
    42. } //<- void UpdateGUIText()