Search Unity

Only run code if not over UI?

Discussion in 'Scripting' started by Corva-Nocta, Sep 25, 2021.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hey all! I'm trying to make a movement system and other stuff where you click to interact with environmental stuff. The problem I have right now is if I click any part of my UI (inventory, buttons, avatar pic, etc) my code still runs that interacts with the world. Usually this means when I click I move to where I click. But I don't want to have that script run when over a UI object.

    How do I check to make sure I'm not over a UI object before running code with a raycast?
     
  2. PizzaPie

    PizzaPie

    Joined:
    Oct 11, 2015
    Posts:
    107
    Code (CSharp):
    1. EventSystem.current.IsPointerOverGameObject()
    Should do the trick.

    By Over GameObject it means gameobject with UI component that can be targeted by raycast.