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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

InvokeRepeating dual output

Discussion in 'Scripting' started by Mariol, Feb 24, 2012.

  1. Mariol

    Mariol

    Joined:
    Dec 22, 2009
    Posts:
    42
    Forgive me, I am a complete beginner but I can't find this answer anywhere. I'm trying to creating a simple countdown timer which I have done. My problem is that the timer seems to print out and count down from two numbers. One, the value I supply to the seconds variable either in the code below or via the Unity component. Two, zero. The first number seems to work as I intend and stop counting down at 0, but the second number will continue to decrease.

    Can someone tell me what I've done wrong? I've put my code in below.

    Thanks

    Code (csharp):
    1.  
    2. var seconds = null;
    3.  
    4. function Start () {
    5.     InvokeRepeating ("Countdown", 1.0, 1.0);
    6. }
    7.  
    8. function OnGUI(){
    9.     GUI.Label(Rect(10, 10, 60, 20), "Time : " + seconds);
    10. }
    11.  
    12. function Countdown () {
    13.     seconds -= 1;
    14.     if (seconds == 0){
    15.         CancelInvoke ("Countdown");
    16.     }
    17. }
     
  2. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    Have you checked to see that this GameObject is the only one you have with this Timer script? If it's somewhere else, it could be decrementing at the same time, but the 2nd one wasn't set to a value and was starting at 0.

    However, if you wanted to have 2 timers running, then this could be a scope issue. From the looks "var seconds" is globally accessible. I don't know Javascript, but I'm going to guess it's likely that if there were 2, they are sharing that seconds value. What would happen then is that Timer1 would call Countdown, decrement seconds, and then see it was 0 and cancel the invoke. However, Timer2 in another object is using that same second value, so when it's Countdown function calls, it does seconds = 0 -1, if (seconds == 0) cancel.

    The shared seconds may not be desired.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, it's the same as in C#, where class variables are an instance. You need to make variables static for them to share the same value.

    Yes.

    This should be:

    Code (csharp):
    1. var seconds = 10; // or some other integer
    or

    Code (csharp):
    1. var seconds : int;
    It can't infer the type if you assign null, and also integers can't be null. The actual problem though is what Mariol said, where you have that script on more than one object.

    --Eric
     
  4. Mariol

    Mariol

    Joined:
    Dec 22, 2009
    Posts:
    42
    Thanks for the advice. I managed to get it working. Barinelg was right, I had more than one timer running :(

    I also took your advice too Eric and changed that null value to an int.

    Much appreciated guys!