Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Conflict on eventSystem

Discussion in 'Scripting' started by MrZeker, Apr 16, 2019.

  1. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Hello, im trying to make a game withouth the mouse, i mean, its not visible its only function is to move the camera), however, even if i disabled all the mouse clicks on keys (fire1,fire2,etc) if i click on the mouse it does stuff.
    what my game uses are menues to navegate with WASD but if i click on the mouse any selected ui button is deselected and i cant re-select stuff. however i found a way to fix it, but it conflicts with another script i have for Tooltips
    what the tooltip scrip does is check if the panel is enabled and then display information based on what button is currently selected.
    NOTE: if i turn off the tooltips, then the script works good, and when i click on something i get to re-select stuff normally, but if i have tooltips turned on then it does nothing. and i get this error on console.
    Also, button , button2, and button3 are never active at the same time, thats why i use them on the script, to make sure that theres always something selected.

    "
    NullReferenceException: Object reference not set to an instance of an object
    Button1.Update () (at Assets/Zeker/Scripts/Button1.cs:297)
    "

    Code (csharp):
    1.  
    2.     void Update()
    3.  
    4.     {
    5.  
    6.        if (tooltipPanel.gameObject.activeInHierarchy == true && gameOver == false)
    7.         {
    8.             if (EventSystem.current.currentSelectedGameObject.GetComponent<Button>() == defendButton)
    9.             {
    10.                 tooltipText.text = "35 Energy.                                 blabablablabalabla DEFENSE";
    11.             } else if (EventSystem.current.currentSelectedGameObject.GetComponent<Button>() == AttackMenu)
    12.             {
    13.                 tooltipText.text = "25 Energy.                            "Attack in various ways blabalbal";
    14. }
    15. //the code continues for 15 more tooltips, but they are all the same code with different wording.
    16.  
    17.    //reparar bug mouse
    18.         if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))
    19.         {
    20.            CatchMouseClicks();
    21.         }
    22. }
    23.    public void CatchMouseClicks()
    24.    {
    25.        if (button.gameObject.activeInHierarchy == true)
    26.        {
    27.            EventSystem.current.SetSelectedGameObject(button);
    28.        }
    29.        else if (button2.gameObject.activeInHierarchy == true)
    30.        {
    31.            EventSystem.current.SetSelectedGameObject(button2);
    32.        }
    33.        else if (button3.gameObject.activeInHierarchy == true)
    34.        {
    35.            EventSystem.current.SetSelectedGameObject(button3);
    36.        }
    37.  
    38.    }
    39.  
    40.  
    41.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    So rather than still using all of the inherent UI objects, such as Button, and then trying to figure out if the mouse is causing the inputs and then counter-acting that and all of the nightmare that's going to be... your best bet is to actually just write a custom input module for the UI system to use: https://docs.unity3d.com/ScriptReference/EventSystems.BaseInputModule.html

    You can probably just copy and paste the official Unity StandaloneInputModule source code and strip away anything related to the mouse: https://bitbucket.org/Unity-Technol...tSystem/InputModules/StandaloneInputModule.cs

    Make sure you disable the StandaloneInputModule component on your EventSystem GameObject and add your own after you're done.

    Good luck!
     
  3. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Hello, i have been busing solving other bugs and stuff. now im gonna focus on this because its getting annoying.
    isnt there any other way to fix it? because it sounds like a lot of work to do. and im really a damned newbye about coding. :(