Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

small count down timer ideas?

Discussion in 'Getting Started' started by Sylvir, Mar 31, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    I have seen a few more complicated timers when i looked it up, but i just need something that will show a bar that goes down for 30 seconds or so. then i can have an action happen at the end of the timer. what do you think would be the best method to get this done?
     
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning @Sylvir,

    you can create a new Slider UI element in scene.

    create a new C# script called countdown, and copy this script into it. and drag onto Slider gameobject.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class countdown : MonoBehaviour {
    7.  
    8.     public float counter = 10.0f;  // declare a float and et to 30 to begin with for the max values
    9.     private Slider mySlider;  // declare a slider object
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         // set reference to slider
    14.         mySlider = GetComponent<Slider>();
    15.         mySlider.maxValue = counter;
    16.         mySlider.value = counter;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.         // decrement counter float with time it takes to draw each frame if its above 0;
    22.         if (counter > float.Epsilon)
    23.         {
    24.             counter-= Time.deltaTime;
    25.         }
    26.         else
    27.         {
    28.             // if the values so low, just set to 0
    29.             counter = 0;
    30.         }
    31.         // set the sliders value to counter value
    32.         mySlider.value = counter;
    33.  
    34.         if (counter == 0)
    35.         {
    36.             Debug.Log("do something cos counter is 0");
    37.         }
    38.     }
    39. }
    40.  
    41.  
    42.  
    change the slders starting value in public variable in inspector on countdown script.
    just done it this morning to see, just creates a simple countdown to get you started :)
     
  3. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    good morning oboshape!

    thank you for the help! this will defiantly help me get going with it. I am still learning both unity and C# but with the great people on here i am picking up the basics and more every day as i keep trying to learn more! hope you have a great day!
     
  4. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    then just calling under one of my if statements should make it so that if conditions are met, then the timer will go, then after it ends i can put the action i want associated with it. (if i understand right) :) will get working on it in just a bit now thanks again!
     
    OboShape likes this.
  5. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    No worries, glad to give a hand.

    basically the IF checks conditions, and if those conditions are met then do what i put here.. (ie in the brackets)

    you can create a function, and call it from there, or if its something small, just pop it in there.

    yea its a brill forum, ive learned soo much from here on my travels, im still learning too..
     
  6. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    could you explain what the float.epsilon is? trying to work with the script now, but unsure what that is effecting
     
  7. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    time.delta time i think i understand that is the seconds that it counts down by, but i am not quiet understand what the float.epsilon effects.
     
  8. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
  9. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    oh okay, thanks! :)
     
  10. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    hmm, i am getting an error trying to set up my script with the timer.. would you mind taking a quick look and seeing if you know what this error means?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class BuildVirus : BaseVariables  {
    6.  
    7.     private float counter = 30.00f;
    8.  
    9.     void Start(){
    10.         // set reference to slider
    11.         mySlider = GetComponent<Slider>();
    12.         mySlider.maxValue = counter;
    13.         mySlider.value = counter;
    14.     }
    15.  
    16.     void Update(){
    17.         VirusCount.text = "Virus Count: " + virus;
    18. //        TechPoints.text = "Tech Points: " + techPoints;
    19.     //    vpc.text = "VPC: " + virusPerClick;
    20.         Debug.Log ("techPoints:" + techPoints);
    21.     }
    22.  
    23.  
    24.  
    25.     public void Clicked(){
    26.         VirusCost = 50;
    27.     //    Debug.Log("techPoints before:"+techPoints);
    28.             if(techPoints >= VirusCost){
    29.             TimeCountDown();
    30.             techPoints -= VirusCost;
    31.  
    32.             virus += 1;
    33.             }
    34.     //    Debug.Log("techPoints after:"+techPoints);
    35.            
    36.         }
    37.  
    38.     public void TechPointsClicked(){
    39.         techPoints += techPointsPerClick;  
    40.         {
    41.  
    42.    
    43.     }
    44.     }
    45.  
    46.  
    47.  
    48.     void TimeCountDown () {
    49.         // decrement counter float with time it takes to draw each frame if its above 0;
    50.         if (counter > float.Epsilon)
    51.         {
    52.             counter-= Time.deltaTime;
    53.         }
    54.         else
    55.         {
    56.             // if the values so low, just set to 0
    57.             counter = 0;
    58.         }
    59.         // set the sliders value to counter value
    60.         mySlider.value = counter;
    61.        
    62.         if (counter == 0)
    63.         {
    64.            
    65.            
    66.         }
    67.     }
    68.  
    69.  
    70.  
    71.  
    72.  
    73.  
    74.  
    75. }
    the error says..


    NullReferenceException: Object reference not set to an instance of an object
    BuildVirus.TimeCountDown () (at Assets/Scripts/BuildVirus.cs:60)
    BuildVirus.Clicked () (at Assets/Scripts/BuildVirus.cs:29)
    UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:110)
    UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574)
    UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716)
    UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
    UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
    UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
    UnityEngine.EventSystems.EventSystem:Update()



    i am trying to work out what is not set.. i have the script i just shared on the button that i click when i have 50 tech points, and then it gets that error..
     
  11. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    i dont know what you have in your base class, as in what variables are inhereted :(

    but for a starter for 10.
    on 17 you have
    Code (CSharp):
    1. VirusCount.text = "Virus Count: " + virus;
    you need to make a public reference to VirusCount, i take it that its a UI element. as it wont know what this is.

    i just had the slider in there as an example, how are you actually implementing the timer?
    kind of sounds like your needing a coroutine at first glance
     
  12. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    oh,

    my base is this right now..

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class BaseVariables : MonoBehaviour {
    6.  
    7.  
    8.  
    9.  
    10.     public Slider mySlider;
    11.     public float counter = 10.0f;
    12.     public int virusPerClick = 1;
    13.     public float virus  = 0.00f;
    14.     public int VirusCost ;
    15.     public int click;
    16.     public float techPointsPerClick = 1;
    17.     public static float techPoints = 0.00f;
    18.     private float baseCost;
    19.     public string itemName;
    20.     public int clickPower;
    21.     public int count = 0;
    22.     public float cost;
    23.     public int SofwareCost = 50;
    24.     public UnityEngine.UI.Text VirusCount;
    25.     public UnityEngine.UI.Text TechPoints;
    26.     public UnityEngine.UI.Text itemInfo;
    27.     public UnityEngine.UI.Text money;
    28.     public static float Money = 0.00f;
    29.     public int moneyPerSoftware = 10;
    30.  
    31.  
    32.  
    33.  
    34.  
    35.  
    36.     // Use this for initialization
    37.     void Start () {
    38.    
    39.     }
    40.    
    41.     // Update is called once per frame
    42.     void Update () {
    43.    
    44.     }
    45.  
    46.  
    47.  
    48.  
    49. }
    the virus count is a text that shows the # of viruses you have, it was working before i tried adding the time element lol :p
     
  13. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    seems to be having trouble in 2 places with this line mySlider.maxValue = counter;
     
  14. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    ah ok, remember when in the example i done the script was attached to the slider, so the getComponent would find the attached Slider component.

    in your case, you have dragged the slider object to the public reference slot, as opposed to what was in the example.
    so comment out line 11 that has the following, thats the line number shown above in your buildvirus script.
    Code (CSharp):
    1. mySlider = GetComponent<Slider>();
    see how that goes :)
     
  15. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    okay, thanks ill try that.. sorry i got it all mixed up, i thought that that was there to make it be able to be slotted in in the interface.. lol ill try that.
     
  16. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    so now the virus gets bought using the tech points, but the slider doesnt do anything.
     
  17. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    just noticed another thing, your declaring counter in your base class, and your inherited class, comment out the one on your BuildVirus.



    ok ive just had a play and try and build something.

    as it stands, this will only go through the timer check once each click, ie lowering by roughly 0.1 each time, you can see this in the inspector value for counter..

    what it sounds like you might be after is a coroutine as this will run through all iterations and yield back to the main code, until exit conditions are met and the routine completes, to implement this try the following...

    replace the TimeCountdown function with the following
    Code (CSharp):
    1.     IEnumerator TimeCountDown () {
    2.         while (counter > float.Epsilon)
    3.         {
    4.             counter-= Time.deltaTime;
    5.             mySlider.value = counter;
    6.             yield return null;
    7.         }
    8.         Debug.Log ("timer reached 0 and resetting!");
    9.         mySlider.value = mySlider.maxValue;
    10.     }
    then you will need to put the following where you would have normally called the function, as what you want is the coroutine to carry on through all the iterations until complete.
    so where you have
    Code (CSharp):
    1. TimeCountDown ();
    replace that with the following
    Code (CSharp):
    1. StartCoroutine( TimeCountDown());
    again this is only for example sake, you will have to ammend and change it to suit your situation by putting a flag in the coroutine somewere to check that its running so you cant call it twice..

    hope that helps a bit more, its hard to give you exact code, as i dont understand how its used in your scene, thats for you to implement, but this should show you results :)

    Rgds
    DaZ
     
  18. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    awesome, thanks for checking into it for me, ill do some reading up on the documentation on coroutines as i am not fermillier with them yet, and then ill give this code structure a try, and tweak it to fit me scene. thanks again for all the help it is very much apreciaited