Search Unity

How to avoid placement of objects with touch on top of UI (button)?

Discussion in 'Scripting' started by zyonneo, Aug 2, 2019.

  1. zyonneo


    Apr 13, 2018
    I am using Image as background which is a UI element.When I select/click a button and again touch on the screen containing the background image another sprite gets instantiated on the screen.After that when I go to click another button again the sprite gets instantiated on top of the button.How to avoid this.

    I want the button to be clicked without instantiating already selected sprite on top of it.

    Code (CSharp):
    1.  if (Input.touchCount > 0 && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    2. {
    3. //As it disable the  instantiation of sprites that comes on the top of the background image.
    4. }
    If I use the above code I cannot instantiate by touching on background image which is a UI.I cannot remove the background image as it is important.This is a 2d project.
    Last edited: Aug 2, 2019
  2. doctorpangloss


    Feb 20, 2013
    Use the IPointerClickHandler pattern for handling touch events. Unity has a built-in system for "consuming" events. In this case, you will ".use" on your pointer event.