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

C# Random Timer on screen display using GUItext

Discussion in 'Scripting' started by MegaPhlegnh, Feb 18, 2015.

  1. MegaPhlegnh

    MegaPhlegnh

    Joined:
    Feb 18, 2015
    Posts:
    6
    Hi all,

    Super beginner Unity C# programmer here looking for some help.

    My overall goal is to have random text with a random timer display to the Gui.

    What I have so far;

    using UnityEngine;
    using System.Collections;

    public class Countdown : MonoBehaviour
    {
    public float timer;
    public float timerLimit = 30.0f;
    public GUIText timerText;

    // Use this for initialization
    void Start ()
    {
    timer = timerLimit;
    SetTimerText ();
    }

    // Update is called once per frame
    void Update ()
    {
    timer -= Time.deltaTime;
    SetTimerText ();
    if (timer <= 0.0f)
    {
    print ("GAME OVER!");
    timer = timerLimit;
    }

    }
    public void SetTimerText()
    {
    timerText.text = "Checkpoint Alarm : " + timer.ToString ("f0");
    }
    }


    This works fine to display "SPECIFIC" text and time,


    What I am playing with (using a List);

    objectTimers = new List<ObjectTimer>();

    objectTimers.Add(new ObjectTimer("Kitchen Alarm: ", Random.Range (20, 120));
    objectTimers.Add(new ObjectTimer("Storage Alarm: ", Random.Range (20, 120));
    objectTimers.Add(new ObjectTimer("Game Room Alarm: ", Random.Range (20, 120));
    objectTimers.Add(new ObjectTimer("Private Party Room Alarm: ", Random.Range (20, 120));
    objectTimers.Add(new ObjectTimer("Shop Alarm: ", Random.Range (20, 120));
    objectTimers.Add(new ObjectTimer("Office Alarm: ", Random.Range (20, 120));
    objectTimers.Add(new ObjectTimer("Resturaunt Alarm: ", Random.Range (20, 120));


    But I can't figure out how to get that randomness to take the place of my Specific from above.

    Any help would be really appreciated.

    Thank you!

    Tom
     
  2. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Please use code tags to help us read your code. :)
     
  3. MegaPhlegnh

    MegaPhlegnh

    Joined:
    Feb 18, 2015
    Posts:
    6
    Sorry! Not sure what a code tag is. Looking it up.
     
  4. MegaPhlegnh

    MegaPhlegnh

    Joined:
    Feb 18, 2015
    Posts:
    6
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Countdown : MonoBehaviour
    5. {
    6. public float timer;
    7. public float timerLimit = 30.0f;
    8. public GUIText timerText;
    9.  
    10. // Use this for initialization
    11. void Start ()
    12. {
    13. timer = timerLimit;
    14. SetTimerText ();
    15. }
    16.  
    17. // Update is called once per frame
    18. void Update ()
    19. {
    20. timer -= Time.deltaTime;
    21. SetTimerText ();
    22. if (timer <= 0.0f)
    23. {
    24. print ("GAME OVER!");
    25. timer = timerLimit;
    26. }
    27.  
    28. }
    29. public void SetTimerText()
    30. {
    31. timerText.text = "Checkpoint Alarm : " + timer.ToString ("f0");
    32. }
    33. }
     
  5. MegaPhlegnh

    MegaPhlegnh

    Joined:
    Feb 18, 2015
    Posts:
    6
    Code (CSharp):
    1. objectTimers = new List<ObjectTimer>();
    2.  
    3. objectTimers.Add(new ObjectTimer("Kitchen Alarm: ", Random.Range (20, 120));
    4. objectTimers.Add(new ObjectTimer("Storage Alarm: ", Random.Range (20, 120));
    5. objectTimers.Add(new ObjectTimer("Game Room Alarm: ", Random.Range (20, 120));
    6. objectTimers.Add(new ObjectTimer("Private Party Room Alarm: ", Random.Range (20, 120));
    7. objectTimers.Add(new ObjectTimer("Shop Alarm: ", Random.Range (20, 120));
    8. objectTimers.Add(new ObjectTimer("Office Alarm: ", Random.Range (20, 120));
    9. objectTimers.Add(new ObjectTimer("Resturaunt Alarm: ", Random.Range (20, 120));
     
  6. MegaPhlegnh

    MegaPhlegnh

    Joined:
    Feb 18, 2015
    Posts:
    6
    OK! found it, sorry about that.

    Tom
     
  7. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Where is this code located? I can't see what you're doing with the objectTimers list. I'm also not sure what ObjectTimer does, since I can only see the CountDown class.

    SetTimerText() sets a very specifc string to your GUI text. You should probably create a clock name member and use that instead of the explicit string you're using now.

    Code (CSharp):
    1.  
    2. public class Countdown : MonoBehaviour
    3. {
    4.     public float timer;
    5.     public float timerLimit = 30.0f;
    6.     public GUIText timerText;
    7.     public string clockName;
    8.  
    9.     public void SetTimerText(){
    10.         timerText.text = string.Format("{0}: {1}", clockName, timer.ToString());
    11. }
    12.  
    This is my best guess with the code provided. I'm assuming that ObjectTimer has something in its constructor that handles some setup.
     
    Last edited: Feb 19, 2015
  8. MegaPhlegnh

    MegaPhlegnh

    Joined:
    Feb 18, 2015
    Posts:
    6
    Hey there JT,


    Thank you for that info,


    Sorry I forgot about that ObjectTimer one. Here goes;

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ObjectTimer
    6. {
    7.  
    8.  
    9.         public string name;
    10.         public int time;
    11.         public bool isStarted;
    12.         public int newTime;
    13.        
    14.         public ObjectTimer(string newName, int NewTime)
    15.         {
    16.             name = newName;
    17.             time = newTime;
    18.             isStarted = false;
    19.         }
    20. }
     
  9. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Sorry. There's a lot of gaps in your code and I'm going to have to make some assumptions to be of any use. What you need to do is grab a reference of one of the ObjectTimers in your objectTimers list, assign that to coutndown, and use that reference for displaying info. Currently I can't see how anything is interacting, or where you objectTimer list lives, and that's what is important. Here's a hint though without going into too much detail, and not incorporating your list at all.

    Also I'm ignoring the use of timerLimit because I don't fully understand what you're trying to do with it. You create a random amount of time but then you cap it at 30 seconds. The below script will create you a timer at 120 seconds and when it is fully counted downit print Game Over. At any time you can replace Countdown.Timer with a new ObjectTimer instance and it'll just keep on truckin' with that new timer.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Countdown : MonoBehaviour
    6. {
    7.     public static ObjectTimer Timer;
    8.     public GUIText timerText;
    9.  
    10.     // Use this for initialization
    11.     void Start]()
    12.     {
    13.         Timer = new ObjectTimer("Alarm", 120);
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         Timer.time -= Time.deltaTime;
    20.         SetTimerText ();
    21.         if (Timer.time <= 0.0f)
    22.         {
    23.             // This will spam your console with Game Over, btw.
    24.             print("GAME OVER!");
    25.         }
    26.  
    27.     }
    28.     public void SetTimerText()
    29.     {
    30.         timerText.text= string.Format("{0}: {1}", Timer.newName, Timer.time);
    31.     }
    32. }
    33.  
    34.  
    So, in a different script you could generate your list of timers (or this script, actually) and then randomly choose one to assign to the static Timer variable.

    I hope that does what you want.