Search Unity

Left Mouse Click Breaks UI Buttons? Why? - SOLVED!

Discussion in 'Editor & General Support' started by JWdell, Jul 23, 2016.

  1. JWdell

    JWdell

    Joined:
    Oct 24, 2014
    Posts:
    54
    I've recently updated my older version of Unity to a newer 5+ version so I could use the Canvas and EventSystem to redo my menu to support keyboard/controller selection. I have read that many people have run into this situation but have not found anyone or any info on a solution.

    My issue is, I've setup everything correctly with a Canvas and EventSystem and my buttons are working fine with the keyboard and controller but the problem is, I can still click on the left mouse button which actually breaks the EventSystem to where you can no longer select any buttons anymore. I know this is not the intended functionality but does anyone know how I can prevent or turn off being about to mouse click? It seems that there would be a simple option to turn this off if you're using the Canvas and EventSystem for keyboard and controller navigation support since it breaks it.

    I'm at a loss here with trying to figure it out since in the Input Manager the Keyboard or Mouse Button is shared under the same tab. I need to disable Mouse Button clicking.

    Hiding and locking the Mouse Cursor does just that, it hides it and makes it not moveable but still clickable with left mouse button which is the issue and breaks EventSystem button navigation to where you have to ctrl-del to close the game since you can no longer navigate to Quit Game button.

    Any hints, help or advice?

    -----

    SOLVED!!
     
    Last edited: Jul 25, 2016
  2. CameronPenner

    CameronPenner

    Joined:
    Mar 7, 2015
    Posts:
    6
    Hey, I've got the same problem. Could you post your solution, so others who find the post can figure it out?
     
  3. JWdell

    JWdell

    Joined:
    Oct 24, 2014
    Posts:
    54
    It was found in another thread but I didn't save the link to it. If anyone see's this in another thread, feel free to post the link and I'll update the OP with original credit as I don't take credit for this fix.

    Just create an empty gameobject and put this script on it.

    It works for my needs but I also had to add to it within my levels for a proper fix. While this works perfectly fine for a menu and fixes clicking the mouse button and breaking button navigation in the button event system, if you have an in game pause menu, you'll need to make a variable in your pause game script at some point that activates/deactivates the gameobject with this script on it when you pause / unpause your game.

    In the future I think this is something Unity should address and make it so that clicking the mouse button doesn't break the event system buttons.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. // If there is no selected item, set the selected item to the event system's first selected item
    5. public class ControllerRefocus : MonoBehaviour
    6. {
    7.     GameObject lastselect;
    8.  
    9.     void Start()
    10.     {
    11.         lastselect = new GameObject();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         if (EventSystem.current.currentSelectedGameObject == null)
    18.         {
    19.             EventSystem.current.SetSelectedGameObject(lastselect);
    20.         }
    21.         else
    22.         {
    23.             lastselect = EventSystem.current.currentSelectedGameObject;
    24.         }
    25.     }
    26.  
    27. }
     
    ZoodlesGaming, Xeisu, KWaldt and 5 others like this.
  4. Rusted_Games

    Rusted_Games

    Joined:
    Aug 29, 2010
    Posts:
    135
    It's amazing how until now with 2018.2 we still need to use this hacks!!! shame on Unity
     
  5. orestesgas

    orestesgas

    Joined:
    Jul 29, 2017
    Posts:
    3
    Oh, thanks for the solution.
     
  6. Xeisu

    Xeisu

    Joined:
    Nov 8, 2020
    Posts:
    2
    @JWdell
    Amazing! Your post is from 2016 and is still the only solution that helped me in 2021 (Using Unity 2020.2.1f1 Personal). Thank you.