Search Unity

UI buttons for animation are causing unexpected problem,please help

Discussion in 'UGUI & TextMesh Pro' started by matthew77, Feb 2, 2018.

  1. matthew77

    matthew77

    Joined:
    Dec 17, 2016
    Posts:
    29
    Hi. I've recently started working with UI buttons. I'm creating a menu of buttons to trigger character animations. So the method I'm using is that in the onclick section of the button I've assigned my character as the object and then the function I'm calling is Animator > Play(string) .

    Ok so this seemed to be working perfectly and I was almost done creating the menu when I noticed a problem. After one of the buttons are clicked and the animation plays when I go back to normal/idle ,and then press the spacebar the character no longer jumps. Instead it cycles through the different UI button animations each time it's pressed.

    I know it's not a problem with the animations themselves because they work as expected and don't interfere with the space function when I access them from a normal keypress.

    Can anyone tell me why this might be happening and how to fix it? I'd be greatfull for any help you might be able to give. Thanks in advance.
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    I would guess the button is still selected and pressed when you hit the spacebar.
    Take a look at your event system in the debug inspector, there you see the currently selected element:
    upload_2018-2-6_14-49-58.png
    You can just change the selected element at any point with EventSystem.current.SetSelectedGameObject(...)
     
    TechDevTom likes this.
  3. TechDevTom

    TechDevTom

    Joined:
    Oct 21, 2011
    Posts:
    33
    Thank you! I finally narrowed my bug down to something to do with the 'onClick' handler I have messing about and your solution helps clear up any stored items in the EventSystem :D
     
  4. nickfagan

    nickfagan

    Joined:
    Oct 27, 2015
    Posts:
    4
    You could also try setting the Navigation field on the buttons from to None:

    Screen Shot 2019-05-02 at 1.16.36 PM.png