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

Dropdown Coroutine error

Discussion in 'UGUI & TextMesh Pro' started by bear_love_honey, Jan 3, 2016.

  1. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    Hello. I got some problem.

    I use Dropdown in panel and use event On value changed.

    I setactive false panel with Dropdown. and get error.

    Coroutine couldn't be started because the the game object 'Dropdown' is inactive!
    UnityEngine.EventSystems.EventSystem:Update()


    If i use standart button and setactive panel false i dont get this error.
    How i can fix error when i use Dropdown?

    Also i dont get error if destroy panel with Dropdown from Dropdown ( if i use on value changed ).





    Before i deactivate panel.
    I StopAllCoroutines(); from Dropdown. This not help too. I got the same error. :(
    Please help.
     
    Last edited: Jan 3, 2016
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    Try setting the dropdown component to enabled = false instead. The error is probably caused by the dropdown transition when disabled.
     
  3. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    Hello thanks for reply.

    I'll make simple script.

    Code (CSharp):
    1.    
    2.     public GameObject testPanel;
    3.     public Dropdown testDropDown;
    4.  
    5.     public void TestActive()
    6.     {
    7.         testDropDown.StopAllCoroutines();
    8.         testDropDown.enabled = false;
    9.         testPanel.SetActive(false);
    10.     }
    11.  
    And got the same Error.

    I'll try use this with testDropDown.StopAllCoroutines(); and without and got same effect.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    Could you file a bug report with an example that creates the problem and post the case id here? I'll take a look tomorrow.
     
  5. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    I never do this. But i'll try. Thanks for reply.
     
  6. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    Hello again.

    Bug ID 758873

    I send project.
    Sorry if make incorrectly bug report message.
    Also i send additional information with screen and script in mail.
     
    Last edited: Jan 3, 2016
    karl_jones likes this.
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    Looks good. I'll take a look tomorrow.
     
  8. Narmer

    Narmer

    Joined:
    Mar 5, 2014
    Posts:
    25
    Hello, I am getting the same error. Any progress or at least workaround?
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    This bug has been fixed. I will check with the UI team to see if its safe to backport to a 5.3 patch release. If you need the fix now you can download the UI source code and implement it yourself, its quite simple.

    Code (CSharp):
    1. diff --git a/Extensions/guisystem/UnityEngine.UI/UI/Core/Dropdown.cs b/Extensions/guisystem/UnityEngine.UI/UI/Core/Dropdown.cs
    2. --- a/Extensions/guisystem/UnityEngine.UI/UI/Core/Dropdown.cs
    3. +++ b/Extensions/guisystem/UnityEngine.UI/UI/Core/Dropdown.cs
    4. @@ -592,7 +592,10 @@ namespace UnityEngine.UI
    5.              if (m_Dropdown != null)
    6.              {
    7.                  AlphaFadeList (0.15f, 0f);
    8. -                StartCoroutine(DelayedDestroyDropdownList(0.15f));
    9. +
    10. +                // User could have disabled the dropdown during the OnValueChanged call.
    11. +                if (IsActive())
    12. +                    StartCoroutine(DelayedDestroyDropdownList(0.15f));
    13.              }
    14.              if (m_Blocker != null)
    15.                  DestroyBlocker(m_Blocker);
     
  10. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    Hello Again. Thanks for reply. Sorry my late answer.

    I'll try disable Dropdown in unity 5.3.4.f1 and get same error.
    Dropdown still cant disable himself, or mb i use him wrong? I dont use any scripts.
    How i can disable dropdown OnValueChanged?



    Also i again put DropDown in Panel and Disable Panel from DropDown, and get same error.

     
    Last edited: Apr 10, 2016
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    You should use a script with a small delay (0.15 seconds) before disabling the dropdown. This gives it time to remove the dropdown list and prevent the error message.So on value changed, wait .15f secs and then disable.
     
  12. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    Thanks i'll will try.
     
  13. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    Hello again.
    I write two simple scripts to enable disable DropDown.

    First script work correctly.

    Code (CSharp):
    1. public class DropDownFirstUtility : MonoBehaviour
    2. {
    3.     #region public
    4.     public Dropdown MyDropDown;
    5.     #endregion
    6.  
    7.     public void Enable()
    8.     {
    9.         #region Enable
    10.         MyDropDown.gameObject.SetActive(true);
    11.         #endregion
    12.     }
    13.  
    14.     public void Disable()
    15.     {
    16.         #region Disable
    17.         StartCoroutine(DisableDropDown());
    18.         #endregion
    19.     }
    20.  
    21.     private IEnumerator DisableDropDown()
    22.     {
    23.         #region DisableDropDown
    24.         yield return new WaitForSeconds(0.15f);
    25.         MyDropDown.gameObject.SetActive(false);
    26.         #endregion
    27.     }
    28. }
    If i init OnValueChanged in Unity Editor all works fine.
    I can disable and enable DropDown.

    In second script i init OnValueChanged from script by AddListener.

    Code (CSharp):
    1. public class DropDownSecondUtility : MonoBehaviour
    2. {
    3.     #region public
    4.     public Dropdown MyDropDown;
    5.     #endregion
    6.  
    7.     #region private
    8.     private List<string> TaskList;
    9.     #endregion
    10.  
    11.     private void Awake()
    12.     {
    13.         #region Awake
    14.         Init();
    15.         #endregion
    16.     }
    17.  
    18.     private void Init()
    19.     {
    20.         #region Init
    21.         TaskList = new List<string>();
    22.         MyDropDown.options.Clear();
    23.  
    24.         TaskList.Add("Commands");
    25.         TaskList.Add("Utilize");
    26.         TaskList.Add("Stop");
    27.  
    28.         foreach (var n in TaskList)
    29.         {
    30.             MyDropDown.options.Add(new Dropdown.OptionData() { text = n });
    31.         }
    32.  
    33.         MyDropDown.transform.FindChild("Label").GetComponent<Text>().text = TaskList[MyDropDown.value];
    34.  
    35.         AddListener();
    36.         #endregion
    37.     }
    38.  
    39.     private void AddListener()
    40.     {
    41.         #region AddListenerAddBuildingsInQueue
    42.         MyDropDown.onValueChanged.RemoveAllListeners();
    43.         MyDropDown.onValueChanged.AddListener((value) => SetCommands());
    44.         #endregion
    45.     }
    46.  
    47.     private void SetCommands()
    48.     {
    49.         #region SetCommands
    50.         switch (TaskList[MyDropDown.value])
    51.         {
    52.             case "Commands":
    53.                 print("Commands");
    54.                 break;
    55.             case "Utilize":
    56.                 print("Utilize");
    57.                 Disable();
    58.                 break;
    59.             case "Stop":
    60.                 print("Stop");
    61.                 break;
    62.         }
    63.         #endregion
    64.     }
    65.  
    66.     private void Disable()
    67.     {
    68.         #region Disable
    69.         StartCoroutine(DisableDropDown());
    70.         #endregion
    71.     }
    72.  
    73.     public void Enable()
    74.     {
    75.         #region Enable
    76.         MyDropDown.gameObject.SetActive(true);
    77.         #endregion
    78.     }
    79.  
    80.     private IEnumerator DisableDropDown()
    81.     {
    82.         #region DisableDropDown
    83.         yield return new WaitForSeconds(0.15f);
    84.         MyDropDown.gameObject.SetActive(false);
    85.         #endregion
    86.     }
    87. }
    All works fine until i'll try disable DropDown. If i disable him i cant enable him again.
    I click on DropDown and nothing happend.



    He dont response on my click, and nothing happend.
    But if i doubleclick near him on empty space he response.

    How i can fix it? Or i init wrong DropDown from script.

    I hope you understand what i mean.
     
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    Its probably because the the dropdown uses the onValueChanged event and you clear all listeners here
    Code (csharp):
    1. MyDropDown.onValueChanged.RemoveAllListeners();
    Try using RemoveListener instead.
     
  15. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    When i remove listener i add him again.
    Code (CSharp):
    1. MyDropDown.onValueChanged.AddListener((value) => SetCommands());
    Code (CSharp):
    1. MyDropDown.onValueChanged.RemoveAllListeners();
    If i dont use RemoveAllListeners he start dublicate all events, when i make UI dynamic. Because sometimes i need reInit options.


    But if i comment this line i still get same problem. DropDown dont response after i close him, and response if i click near him in empty space.
    Its look like he drop option list and make they transparent.
     
  16. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    I'm not sure what you mean. The script works fine for me. Can you share your project?
     
  17. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    Yes ofc, i make tested proj with two scripts.

    I put empy object with script to scene, and make Button to ReEnable DropDown.

    I add file to yandex disk

    https://yadi.sk/d/ABp04bKmqtq3U


    If you click to "Utilize" in DropDown you disable him, after this click on button to reenable him.
     
  18. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    Increase the delay to 0.2. The dropdown is destroyed after .15 but sometimes you disable just before it gets destroyed. This is a bug we have fixed(see earlier in this post).
     
  19. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    Yes thanks now all works fine, i thought what 0.15 ms enough.