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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Error when seting InputField Interactable false in Callback "OnEndEdit"

Discussion in 'UGUI & TextMesh Pro' started by ChiefInspectorEvenLonger, Sep 19, 2018.

  1. ChiefInspectorEvenLonger

    ChiefInspectorEvenLonger

    Joined:
    Jul 18, 2013
    Posts:
    5
    Hello,
    I´m getting the following Error when i try to set the InputField to not Interactable when finished writing:
    Attempting to select while already selecting an object.
    UnityEngine.UI.Selectable:set_interactable(Boolean)

    It only appears when you click outside of the Field, but NOT if you press Enter to finish
    Is there a way to get around that Issue? I want the Field not be able to edited except when the player
    rightclicks on it and when finished it goes back to not interactable!

    How can i get rid of the annoying Error?

    Thanks for help in advance!

    Code (CSharp):
    1.   public InputField NameText;
    2.  
    3.     void Start() {
    4.  
    5.         NameText.onEndEdit.AddListener(OnNameEdit);
    6.         NameText.interactable = true;
    7.  
    8.         EventTrigger trigger = NameText.GetComponent<EventTrigger>();
    9.         EventTrigger.Entry click = new EventTrigger.Entry {
    10.             eventID = EventTriggerType.PointerClick
    11.         };
    12.         click.callback.AddListener((data) => {
    13.             PointerEventData ped = ((PointerEventData)data);
    14.             if (ped.button == PointerEventData.InputButton.Left)
    15.                 OnSelectTradeRoute();
    16.             if (ped.button == PointerEventData.InputButton.Right)
    17.                 OnInputFieldClick();
    18.         });
    19.         trigger.triggers.Add(click);
    20.     }
    21.  
    22.     private void OnSelectTradeRoute() {
    23.         onSelect?.Invoke(tradeRoute);
    24.     }
    25.  
    26.     private void OnInputFieldClick() {
    27.         if (NameText.readOnly)
    28.             return;
    29.         NameText.interactable = true;
    30.         NameText.Select();
    31.     }
    32.  
    33.     private void OnNameEdit(string name) {
    34.         tradeRoute.Name = name;
    35.         NameText.interactable = false;
    36.     }
     
    Last edited: Sep 19, 2018
  2. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    328
    Did you ever get to the bottom of this? I have the same problem and `m_InputField.DeactivateInputField();` doesn't help. Not sure what to do top sort it. For me the problem is while editing the input field, hitting return works fine, but clicking off it onto something else throws that same error.
     
    Chazmus likes this.
  3. atulpateltmspl

    atulpateltmspl

    Joined:
    Feb 3, 2020
    Posts:
    7
  4. Chazmus

    Chazmus

    Joined:
    Jun 12, 2018
    Posts:
    3
  5. Chazmus

    Chazmus

    Joined:
    Jun 12, 2018
    Posts:
    3
    Not exactly ideal, but a workaround is to disable the InputField component as opposed to setting interactable to false

    So in the example above:
    Code (CSharp):
    1. private void OnNameEdit(string name)
    2. {
    3.         tradeRoute.Name = name;
    4.         NameText.enabled= false;
    5. }
     
  6. Yiming075

    Yiming075

    Joined:
    Mar 24, 2017
    Posts:
    29
    Here got a better solution for this. Just wait some time and the error is gone.

    Code (CSharp):
    1. IEnumerator DisableInput (InputField input)
    2. {
    3.     yield return new WaitForEndOfFrame();
    4.     input.interactable = false;
    5. }
    6.  
     
    shogoun94 likes this.
  7. BasicDevNoob

    BasicDevNoob

    Joined:
    Apr 27, 2022
    Posts:
    1
    10 Years later anyone find a solution? the solution of waiting for the end of the frame works, but it seems like a workaround for a simple error, instead of a fix