Search Unity

How to distinguish between click on background and UI?

Discussion in 'Getting Started' started by Jonatron5, Nov 30, 2020.

  1. Jonatron5

    Jonatron5

    Joined:
    Nov 30, 2020
    Posts:
    1
    So I'm super new to unity,

    I have a 2D project with a colored square as a background, overwhich I have 2 other shapes acting as buttons.

    I have a script written thats tied to the back ground to allow me to click and draw lines, but one problem I ran into , is when I click on my UI buttons, the script to draw lines fires. so I'm looking for some way to distinguish the click handiler from the 2 locations.

    I have 2 thoughts that seem slightly "hacky"

    1. being to reduce the size of the background component, and put another component underneath it that is the same color but does not have my script applied to it.
    2. do some math, and define a conditional IF statement in the scripting to ignore any clicks within the area of the screen reserved for those buttons.


    I think those 2 ideas might work, but they don't feel like the proper solution.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You haven't described how your "click background to draw lines" code works. I'm guessing you have some script that is watching Input.GetMouseButton to determine when to draw.

    Instead, you should put a background panel in your UI that catches clicks via an EventTrigger script. Then the event system will properly work out which click events are meant for that, and which are instead going to some higher-level element like a button.