Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Bug Toggle compent's OnValueChanged event not working.

Discussion in 'UGUI & TextMesh Pro' started by HeyBishop, Jun 3, 2021.

  1. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    I have a Toggle. When the user presses it, I see "IsOn" checking on/off. But the events in OnValueChanged are not triggered.

    Here's a demonstration of the toggle not working:


    And here are the functions I'm calling from the OnValueChanged:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestDebugOfToggle : MonoBehaviour
    4. {
    5.  
    6.     public void IsThisWorking(bool value)
    7.     {
    8.         if (value)
    9.         {
    10.             print("Yes, it's working. Truth.");
    11.         }
    12.         else
    13.         {
    14.             print("Yes, it's still working, and we're false.");
    15.         }
    16.     }
    17.  
    18.     public void SaySomething()
    19.     {
    20.         print("Something");
    21.     }
    22. }
     
  2. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    Sorry - I just realized, I'm not sure if should have used the Bug prefix in the thead title, or Help Wanted. There are threads with similar issues, but those are years and years old.
    Am I missing something? Or is this a years-old Unity bug?
     
  3. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    Hi there, just pinging back.
    Does anyone have any ideas?
     
  4. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    I've changed the handle of this thread's title to 'bug'.
    Hoping for some support.
     
  5. bfoddy

    bfoddy

    Joined:
    Mar 27, 2012
    Posts:
    85
    I got the same issue, fwiw. It works fine in build, or if I attach the debugger to the editor, but it does not work in play mode.
     
  6. bfoddy

    bfoddy

    Joined:
    Mar 27, 2012
    Posts:
    85
    If I toggle the 'isInteractible' property on the toggle component, at runtime, then it starts to work properly. So there is clearly a unity bug.

    I found a repro - it happens if your toggle is inside a scroll view. I filed a bug report.
     
    Last edited: Nov 8, 2021
  7. bfoddy

    bfoddy

    Joined:
    Mar 27, 2012
    Posts:
    85
    Ok for anyone else who finds this - a workaround while we wait for Unity to fix the issue is to set your toggles/sliders to send events in Editor + Runtime. Toggling the isInteractible property in a script won't work.
     
  8. kvabakoma

    kvabakoma

    Joined:
    Jun 20, 2016
    Posts:
    5
    Old one but just had the same issue and found what was going on:

    Watch this:


    When selecting the method in the OnValueChange field you should select the from the Dynamic Bool options.

    upload_2023-2-18_23-6-44.png
     
    lilrodent likes this.
  9. xthewhitel

    xthewhitel

    Joined:
    May 12, 2018
    Posts:
    1
    Hi, I just bumped into this (or a similar) issue myself and I've found the problem.

    UNITY 2019.4.31f1

    It seems that my issue was related to the Experimental "Enter Play Mode" settings:

    upload_2023-6-27_16-3-1.png

    Having those settings on result in the toggles not working at all until you "wake them up" by changing ANY properties of the toggle at runtime.

    Haven't tested this on any other Unity versions, and this may be another type of bug, but hope posting this here will help someone. ♥
     
  10. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    I've been going nuts trying to figure out why OnValueChanged is being invoked even when the gameobject is inactive!
    I guess at some point I enabled that Enter Play Mode Options. If it weren't for your post on the very same thread I started two years ago, I'd still be pulling my hair out. Thank you @xthewhitel!