- Home /
IsPointerOverGameObject() is true for wrong object sometimes
I have some code for a tooltip UI object that I want trigger after about a half a second. It's working except that I have a bug, caused by IsPointerOverGameObject(), where sometimes the tooltip will pop up over the wrong UI object. This only happens if you move the cursor from one object to the next at a specific speed; otherwise it works perfectly. But it happens way more often than I would like.
The code looks like this:
public void OnPointerEnter(PointerEventData eventData)
{
StartCoroutine(WaitToPop());
Debug.Log(name+ " OnPointerEnter");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log(name + " OnPointerExit");
toolTipPanel.SetActive(false);
}
IEnumerator WaitToPop()
{
yield return waitTime;
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log(name + " IsPointerOverGameObject()");
panelText.text = tooltipText;
toolTipPanel.transform.position = tooltipPosition;
toolTipPanel.SetActive(true);
}
}
The console looks like this:
And here is a gif of the offending code in action:
If necessary, I can upload a simple version of the project somewhere.
Have you thought of using a event trigger to activate a on exit and on enter function? Or would you not be able to engage the script?
@JxWolfe Sorry for the late response. $$anonymous$$y gmail account was pushing all my emails from unity over to "promotions" so I never saw them. I'll try to look at this and see if it's possible.
Answer by Fragmental · Apr 22, 2018 at 05:01 PM
I got it working correctly by using a bool instead of IsPointerOverGameObject() so fixed code reads:
public void OnPointerEnter(PointerEventData eventData)
{
StartCoroutine(WaitToPop());
Debug.Log(name+ " OnPointerEnter");
hasExited = false;
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log(name + " OnPointerExit");
toolTipPanel.SetActive(false);
hasExited = true;
}
IEnumerator WaitToPop()
{
yield return waitTime;
if (!hasExited)
{
Debug.Log(name + " IsPointerOverGameObject()");
panelText.text = tooltipText;
toolTipPanel.transform.position = tooltipPosition;
toolTipPanel.SetActive(true);
}
}
but I still have no idea why IsPointerOverGameObject() is weird like that