Search Unity

Resolved New Input System - UI Navigation Type Input - Determine which button was clicked onSubmit()

Discussion in 'Input System' started by maris-otter, Jul 1, 2021.

  1. maris-otter

    maris-otter

    Joined:
    Jun 10, 2020
    Posts:
    11
    Hi all,

    I'm working on a game with keyboard navigation to navigate the ui, which is a series of buttons. Trying to use the new input system for this but I cannot determine in my scripts what button was selected when enter (Submit) was pressed. Here's what I have setup:

    Input Actions: Navigation (Up, Left, Down, Right arrow keys), Submit (Enter).
    Event System Object: Input System UI Input Module setup with Move + Submit pointing to above input actions
    Game Controller script: Player Input component added, which Behavior: Send Messages

    In my script I have the following method to respond to Submit button and I want to get the text of the button to know what was selected.
    Code (CSharp):
    1. void OnSubmit(InputValue value)
    2.     {
    3.         GameObject go = (GameObject)value.Get();
    4.         Button buttonClicked = go.GetComponent<Button>();
    5.         if (value.isPressed)
    6.         {
    7.             Debug.Log(buttonClicked.GetComponentInChildren<Text>().text);
    8.  
    9.         }
    10.     }
    However if I try that out, I get 'InvalidCastException: Specified cast is not valid.' for the game object casting. I feel like I must be missing something here, not sure how to determine what button was pressed in the code.

    Appreciate any help. Thanks!
     
  2. maris-otter

    maris-otter

    Joined:
    Jun 10, 2020
    Posts:
    11
  3. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    325
    Well, first and foremost, InputValue.Get() returns the value of input in the form of a system.object.

    This value can only be a struct — such as an int, float, Vector2, etc.
    Casting from this system.object (which is a struct) to a GameObject is impossible, which is why you're getting that error.

    As for how to fix it — I recommend using InputValue.Get<ExpectedValueType>() instead. Like this:
    Code (CSharp):
    1. float value = InputValue.Get<float>();
    This version of the .Get() method doesn't create garbage and it explicitly defines the expected value-type you want from the InputValue class.
    Just make sure that the InputAction in question is type 'value' or 'pass-through', and that the expectedValueType matches the type you use in the InputValue.Get<T>().


    As for the rest of the code — you'll probably need to make some changes.
    But hopefully this should clear up the error!
     
    Last edited: Jul 15, 2021
    maris-otter likes this.
  4. maris-otter

    maris-otter

    Joined:
    Jun 10, 2020
    Posts:
    11
    Thanks very much for your help. I'm able to get some output now from the button press. However, it just returns 1, do you know how I can determine what button was selected at the time of UI submit event? Heres my code:

    Code (CSharp):
    1. void OnSubmit(InputValue value)
    2.     {
    3.         float input = value.Get<float>();
    4.         Debug.Log(input);
    5.  
    6.     }
    Thanks!
     
  5. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    325
    Happy to help. :D

    Hmm, I'm not sure if I can help unless I know the specific UI solution that you are using.
    Are you using Unity UI?
    If so, I believe EventSystem.current.currentSelectedGameObject will be suitable for your needs. (Newest documentation version)

    Also, you may want to check out this thread. There seems to be a lot of useful knowledge in there that may pertain to some of the stuff you're trying to do that could help you out in your glorious UI journey. :)
     
    Last edited: Jul 18, 2021
    Nintendo-Silence and maris-otter like this.
  6. maris-otter

    maris-otter

    Joined:
    Jun 10, 2020
    Posts:
    11
    Thats it! I'm finding the game object now. Thanks so much
     
    NotaNaN likes this.