0

Note the mentioned link that makes this link a duplicate is asking about "How to detect UI object on Canvas on Touch in android?", which is not what my question is about.


I have a scene that only has Unity UI elements.

the UI elements are (essentially) a list of Images, each with a Text as a child.

The Text is a word.

I then created a separate Image (background) with Text, that I want to appear when the mouse is hovering over the particular word, then disappear when the mouse is not hovering over the particular word.

Each UI item has been manually created in the scene.

The issue is that options "OnMouseEnter" and "OnMouseExit" don't work.

my script:

using UnityEngine;
using UnityEngine.EventSystems;

public class MouseEnablePopupWindow : MonoBehaviour
{
    public GameObject popupWindowObject;

    private void Start()
    {
        popupWindowObject.SetActive(false);
    }

    private void Update()
    {
    }

    private void OnMouseEnter()
    {
        print("OnMouseEnter");
        popupWindowObject.SetActive(true);
    }

    private void OnMouseExit()
    {
        print("OnMouseExit");
        popupWindowObject.SetActive(false);
    }
}

The only items in the scene are UI items (canvas, image, text).

The Start method successfully makes the reference game object as inactive. but hovering over or away does nothing.

I'm wondering why the "OnMouseEnter", etc., mouse input handling code doesn't work. Every post I've found on the web regarding the subject involves writing a completely different way of handling input.

Why won't the built-in functionality work? Any ideas? Thanks.

Chris
  • 21
  • 2
  • Don't attach a collider to a UI component. Collider/Collider2D are used on MeshRender and SpriteRenderer. – Programmer Jun 03 '18 at 00:55
  • I removed the collider, still no difference. – Chris Jun 03 '18 at 01:10
  • I also looked through https://stackoverflow.com/questions/41391708/how-to-detect-click-touch-events-on-ui-and-gameobjects and none of the suggestions there solved this particular problem. – Chris Jun 03 '18 at 01:10
  • You need to mention what UI component you are using. Also Edit your question and add the new code you used that's not working. Before you do that, I suggest you read the *"Troubleshooting the EventSystem"* section from the duplicate. – Programmer Jun 03 '18 at 01:16
  • Note that I asked you to edit your question and add the new code you got from the duplicate that's not working. You haven't done so. Without seeing your new code, it's not possible to help you – Programmer Jun 03 '18 at 18:56
  • I haven't changed my code, so there isn't a reason for me to write different code. I looked at the different code examples in the referenced duplicate, and they are either handling different input situations (e.g., touch on android), or are a complete re-write of code attempting to do what the methods "OnMouseOver", etc., are supposed to be able to do by default. I edited my question, and unless I plan on rewriting the entire mouse input instead of using built in functionality of Unity, my code shouldn't have to change. – Chris Jun 04 '18 at 02:19
  • *"I haven't changed my code, so there isn't a reason for me to write different code."* Your current code is not how to detect mouse actions on UI objects. The duplicate shows how to do that which works with the UI, Mesh and Sprite with colliders. Your second comment claimed that you have tried this but you have not. Alright then, don't change it but don't ask the-same question again. Goodluck and happy coding! – Programmer Jun 04 '18 at 02:24
  • maybe you can point out what is inherently wrong with the code I wrote rather then telling me to simply change it to something else. – Chris Jun 04 '18 at 02:29
  • https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseEnter.html --> [OnMouseEnter] called when the mouse enter the GUIElement or Collider. The corresponding OnMouseOver function is called while the mouse stays over the object, and OnMouseExit is called when it moves away. – Chris Jun 04 '18 at 02:33
  • "I haven't changed my code, so there isn't a reason for me to write different code..." in my post. sorry for the confusion. – Chris Jun 04 '18 at 02:34

0 Answers0