Search Unity

Question Selecting a button with a keyboard press, not the mouse, will skip one list item. Any advice?

Discussion in 'UGUI & TextMesh Pro' started by Th0masThe0bvious, Mar 26, 2023.

  1. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    I happened upon an unexpected "glitch" in a program I've been making, and it's obvious enough why this is happening, but what's not obvious is how I can recode this to fix it.

    Here is how the program is supposed to work: A number of purchasable items are listed, and while there are many different things attached to the each, the ones I want to focus on are the description text I included for each, which uses a list format. The way I coded this originally, the first line of description text would be displayed immediately by clicking on the button, and then all of the buttons would disappear, and repeatedly clicking the mouse anywhere or pressing the jump button would continue advancing through the descriptive text until it finished, at which point the buttons reappeared and another purchase could be made. Here is how I coded that process, beginning with the void that I attached a button to.

    Code (CSharp):
    1.  public void SelectItem1()
    2.     {
    3.         if (shopper.GetComponent<Player>().CheckMoney() < item1Cost)
    4.         {
    5.             HUD.dialogueText.text = ShopNotAffordText;
    6.         }
    7.  
    8.         if (shopper.GetComponent<Player>().CheckMoney() >= item1Cost)
    9.         {
    10.             button1.GetComponent<Image>().enabled = false;
    11.             button2.GetComponent<Image>().enabled = false;
    12.             button3.GetComponent<Image>().enabled = false;
    13.             button4.GetComponent<Image>().enabled = false;
    14.             button5.GetComponent<Image>().enabled = false;
    15.             button1.GetComponent<Button>().interactable = false;
    16.             button2.GetComponent<Button>().interactable = false;
    17.             button3.GetComponent<Button>().interactable = false;
    18.             button4.GetComponent<Button>().interactable = false;
    19.             button5.GetComponent<Button>().interactable = false;
    20.             //button1.GetComponentInChildren<Text>().enabled = false;
    21.             shopper.AlterMoney(-item1Cost);
    22.             listInt = 0;
    23.             HUD.dialogueText.text = item1DialogueClips[listInt];
    24.         currentItem = shopTalk.ITEM1;
    25.         }
    26.     }
    27.  
    28.  
    Note those last two lines of code; they call the UI to play the first line of the description now, before they transition into a finite state. Now to that state, in Update:

    Code (CSharp):
    1. if (currentItem == shopTalk.ITEM1)
    2.         {
    3.             if (Input.GetButtonDown("Jump"))
    4.             {
    5.                 if (listInt < item1DialogueClips.Count - 1)
    6.                 {
    7.                     listInt += 1;
    8.                     HUD.dialogueText.text = item1DialogueClips[listInt];
    9.                 }
    10.                 else
    11.                 {
    12.                     currentItem = shopTalk.NONE;
    13.                     button1.GetComponent<Image>().enabled = true;
    14.                     button2.GetComponent<Image>().enabled = true;
    15.                     button3.GetComponent<Image>().enabled = true;
    16.                     button4.GetComponent<Image>().enabled = true;
    17.                     button5.GetComponent<Image>().enabled = true;
    18.                     button1.GetComponent<Button>().interactable = true;
    19.                     button2.GetComponent<Button>().interactable = true;
    20.                     button3.GetComponent<Button>().interactable = true;
    21.                     button4.GetComponent<Button>().interactable = true;
    22.                     button5.GetComponent<Button>().interactable = true;
    23.                     EventSystem.current.SetSelectedGameObject(null);
    24.                     EventSystem.current.SetSelectedGameObject(button1);
    25.                 }
    26.             }
    27.         }
    Note the "-1" part is just there to prevent an error caused by the list int going briefly over that list's length.

    Now, all of the above worked smoothly when just executed by clicking the mouse. However, when I made it compatible with the keyboard input, it didn't work as well. Pressing the jump button to select a purchasable item also counts as the first jump press in the subsequent "Item 1" state, so the game will skip over the first item in the list now. Note also that I have tried changing it to "GetButtonUp", without much improvement.

    Now, if push comes to shove, I could just cheese this one by making the list one longer and moving each item one forward, but is there a more code/input-related way to fix it?