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. Dismiss Notice

Auto-select and highlight first button of menu

Discussion in 'UGUI & TextMesh Pro' started by msklywenn, Sep 22, 2016.

  1. msklywenn

    msklywenn

    Joined:
    Nov 28, 2014
    Posts:
    67
    Hello,

    I have a simple menu with a question and two answers represented by two buttons. The two buttons are animated, so I set the transition type to "Animation". On the first button I added a script that does "GetComponent<Selectable>().Select()" in OnEnable() so that button is selected directly when the menu appears (it is not the first menu so I can't use the first selected value of EventSystem)

    My problem is : the button is indeed selected (if I press the submit button, everything in OnClick() is called) but the state is set to normal instead of highlighted. If I move right, the second button gets highlighted, if I move left, the first button gets highlighted as it should have been from the start.

    How can I force the first button to be selected ? Isn't that the purpose of the Select() method ?

    My button's animator
    upload_2016-9-22_17-26-32.png

    upload_2016-9-22_17-30-9.png

    upload_2016-9-22_17-30-39.png

    upload_2016-9-22_17-32-31.png

    Also, if I press submit with the button not highlighted (only selected), OnClick() is called but nor Disabled nor Pressed are sent to the animator.

    Is this a bug in unity ?

    Lastly, Idle and Active are animations with 0 length and transitions of 0.1 seconds, still, I can't quickly change from one button to the other, often, it will take more than one second before the joystick movement is registered.

    upload_2016-9-22_17-35-57.png

    BTW: I use a custom input module which is the standalone input module with everything mouse related removed.
     
  2. msklywenn

    msklywenn

    Joined:
    Nov 28, 2014
    Posts:
    67


    As can be seen here, neither buttons are highlighted and it's only when I press move right that "no" gets selected and highlighted.
     
  3. msklywenn

    msklywenn

    Joined:
    Nov 28, 2014
    Posts:
    67
    I found a crappy solution. But first some background : so if you use the generated controller, everything works as it should. The problem here is that I had a "open" animation played before any other. For some reason, even if I set the open->idle transition to be on the condition of "normal" to be triggered, the transition is taken but the normal trigger isn't reset.
    The solution : start the animator, wait a bit, enable the button.
    Still feels like a bug in unity, if someone could explain what really is going on, I would be very interested.
     
  4. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Maybe this will help you, add it to your menu script
    Code (CSharp):
    1.         GameObject FTPno = GameObject.Find ("Button name (No)");
    2.         if (FTPno) {
    3.             EventSystem.current.SetSelectedGameObject (FTPno);
    4.         }
     
    Madsen87 and gigazelle like this.
  5. msklywenn

    msklywenn

    Joined:
    Nov 28, 2014
    Posts:
    67
    It's equivalent to
    GetComponent<Selectable>().Select()
    I use, but thanks :)
     
  6. Barry100

    Barry100

    Joined:
    Nov 12, 2014
    Posts:
    200
    Attach this to your first button.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class FirstButton : MonoBehaviour
    7. {
    8.  
    9.     Button button;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         button = GetComponent<Button>();
    14.         button.Select();
    15.     }
    16.  
    17.  
    18. }
     
    mprimo and Darotri like this.
  7. kidmethuselah

    kidmethuselah

    Joined:
    Jan 14, 2021
    Posts:
    16

    This function only calls once though