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

Very strange behavior using UI button on click ()

Discussion in 'Scripting' started by Gustorvo, Nov 18, 2017.

  1. Gustorvo

    Gustorvo

    Joined:
    Oct 26, 2017
    Posts:
    32
    SOLVED: Updated Unity to version 2017.2

    OnClick.png

    When adding more than 1 task to the execution list of UI button On Click() manager, the execution of those tasks gets unpredictable behavior. My countdown script counts twice as fast when there is more than one task added to that list (se the attached image). When I remove one task, the code runs as it should (prints the countdown to the screen every second 5..4..3..2..1 etc). Unity Bug?

    Code (CSharp):
    1.    public IEnumerator Countdown()
    2.     {
    3.         while (timeLeft > 0)
    4.         {
    5.             yield return new WaitForSeconds(1.0f);
    6.             countText.text = timeLeft.ToString("f0");
    7.             timeLeft--;        
    8.         }
    9.     }
    10.  
    11.     public void StartCountDown()
    12.     {
    13.         StartCoroutine("Countdown");
    14.     }
     
    Last edited: Nov 19, 2017
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Never heard of that, nor experienced it. That would be pretty weird, if that's the only change.
     
  3. Gustorvo

    Gustorvo

    Joined:
    Oct 26, 2017
    Posts:
    32
    Well, try it yourself and you'll see what I mean.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    I tried it, did not get the same result. Probably something else happening?

    Try logging in your StartCoroutine and check that it's only started once.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Tested in Unity 2017.2.0p1 and works the same with multiple or single behaviors. Tried it with 1, 2, and 3 OnClick and it all did the same.
     
  6. Gustorvo

    Gustorvo

    Joined:
    Oct 26, 2017
    Posts:
    32
    SOLVED: Updated Unity to version 2017.2
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Good to know. I was wondering if it might have been a bug in a certain version, as that sometimes happens. Glad the update fixed it.