Search Unity

Unity UI Cant detect if pointer is over UI buttons

Discussion in 'UGUI & TextMesh Pro' started by WilliamDaniels, Jun 13, 2018.

  1. WilliamDaniels

    WilliamDaniels

    Joined:
    May 27, 2018
    Posts:
    33
    Hi, i have a UI which is a background and title. I also have another UI with a button to toggle music. When the user touches the screen, it fades into level one.

    At the moment its running that function even when i click on the music button which i dont want.

    The go to would be this:

    Code (CSharp):
    1. if ((Input.touchCount > 0) && Input.GetTouch(0).phase == TouchPhase.Began && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    2.     {
    3.             StartCoroutine(loadNextLevel());
    4.     }
    But as the background is also part of the UI, this will not work because ill always be over a UI element.

    Ive looked for ways i can target specific UI elements but i cannot get anything working.

    I did also have the idea to have a variable called buttonClicked that will be true when the button is clicked and then only go to the next level when its false but unfortunatly the button click event runs after the next level function
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    If I understand it correctly you have two Canvases and probably two Event Systems which are displayed at the same time... and they both work independently. And you want not to click two buttons (one in each of the canvases) at the same time. Is this correct?

    I am not sure if this actually would work as I described it... But if this is the problem, why don't you use the same Event System for all UI?