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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how to check if mouse is not on ANY UI element?

Discussion in 'UGUI & TextMesh Pro' started by dworm, Jul 11, 2015.

  1. dworm

    dworm

    Joined:
    Jun 17, 2015
    Posts:
    74
    i have an interface with various buttons and stuff but my mouse serve other functions in game, which i want to disable if the click happens on ui elements

    how can i check if mouse is clicking on empty space?
     
  2. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    Like this

    Code (csharp):
    1.  
    2. eventSys = GameObject.Find("EventSystem").GetComponent<EventSystem>();
    3.  
    4. public void OnMouseDown()
    5. {
    6.   if (eventSys.IsPointerOverGameObject())
    7.   {
    8.    return; // exit out of OnMouseDown() because its over the uGUI
    9.   }
    10.  
    11.   // Do stuff here
    12.  
    13. }
    14.  
    15.  
     
    dworm likes this.
  3. dworm

    dworm

    Joined:
    Jun 17, 2015
    Posts:
    74
    it works but it also screws some other collision...
    any reason why only this code alone could make things that are thousand miles apart collide?
     
  4. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    The only thing that happens here is a check to see if the mouse is over the uGUI.
    It must be the way you worked it into your code.
    I just used OnMouseDown() as an example, you would need to put the if() in the locations that makes sense to your program.
     
  5. dworm

    dworm

    Joined:
    Jun 17, 2015
    Posts:
    74
    yeah i didnt use on mouse down i just put the if, anyway i remade all and it works without that "bug" but it was really wierd i had objects colliding from very far away dunno, not anymore luckily
     
  6. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    Programming can be mysterious sometimes, :) good luck!
     
  7. DanyOctrome

    DanyOctrome

    Joined:
    Sep 21, 2017
    Posts:
    1
    Man, I love you right now. This also works with UI Toolkit.