Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Can't read mouse hover over UI object.

Discussion in 'Scripting' started by IRNoob, Jan 12, 2023.

  1. IRNoob

    IRNoob

    Joined:
    Jan 26, 2022
    Posts:
    9
    Hi there,
    Can somebody help me with this issue. I'm trying to follow a tutorial on creating a basic inventory system, and I'm getting bogged down in one of the early prototyping steps. The particular step involves creating a basic canvas with item slots made from Raw Image UI objects. And then testing whether, or not, the mouse cursor is hovering over said Inventory UI slots.

    Utilizing this set up, I'm not getting any recognition that the cursor is hovering over the slot. The code is attached to the slots themselves. so each slot has this code. It all seems super simple, so I was surprised when it didnt work. I'm using Unity 2021.3. The tutorial is using unity 2019, or maybe 2018. Is this method outdated? I did find another tutorial using Unity 2020.xx and the same method.

    The slot Raw Images on the canvas are just "as is" I didnt add any components, other than this code, to them. As I didnt see any being added in the tutorial.

    Let me know if anyone wants me to follow up with any screen shots. Any help here is super appreciated! hope everyone is having a fantastic week! and Happy 2023!

    using UnityEngine;
    using UnityEngine.EventSystems;

    public class Slot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    {
    public bool hovered;

    void Start()
    {
    hovered = false;
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
    hovered = true;
    Debug.Log("Hovering");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
    hovered = false;
    Debug.Log("Stopped Hovering");
    }
    }
     
  2. IRNoob

    IRNoob

    Joined:
    Jan 26, 2022
    Posts:
    9
    Further Info:
    I tried doing the same code in a brand new project... and of course it worked. I also tried redoing it from scratch in my main project but that didnt help.

    So this failure to work I think may be driven by some setting in the original project. Any Ideas would be super appreciated! I looked at the package list installed in both projects. They have the same UI package installed, and both same version.

    The project that I'm trying to do this in, was originally created as a Unity 2020.xx project and has been updated to Unity 2021.3 (current LTS).
     
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    This could be because of your choice of input system.

    Check this in Project Settings > Player, in the "Other Settings" section

    upload_2023-1-13_11-20-24.png

    I am very fuzzy on the specifics here, so I can't confidently say which functions and classes go with the old and new input systems, unfortunately.
     
  4. IRNoob

    IRNoob

    Joined:
    Jan 26, 2022
    Posts:
    9
    @chemicalcrux I got very excited for a minute. But no avail. Really appreciated the suggestion.

    They were both set to "Input Manager (Old)"... tried changing the offending project to "Both", and "New" ... just in case sourcery lol. But I'm no Harry Potter.

    Please let me know if you have any other suggestions! Can at least cross this off the list. Thank you again for taking the time to respond.
     
  5. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Do any UI interactions work? If they're all broken, you probably deleted the EventSystem object that gets generated in the scene when you create a UI element:

    upload_2023-1-13_11-52-43.png

    If you're instantiating a bunch of prefabs, and never actually manually created a button/etc. in the scene, then it might never have been created in the first place.

    If so, try creating the event system object. It's under GameObject > UI > EventSystem
     
    IRNoob likes this.
  6. IRNoob

    IRNoob

    Joined:
    Jan 26, 2022
    Posts:
    9

    MY HERO!!!!

    This was the issue. I inadvertently deleted the UI Eventsystem Object. Thanks so much for taking the time to follow up @chemicalcrux. I REALLY appreciate it! :):):)
     
    chemicalcrux likes this.
  7. IRNoob

    IRNoob

    Joined:
    Jan 26, 2022
    Posts:
    9

    Interestingly, for anyone else also having this problem... after fixing it per @chemicalcrux's suggestions. It would not work with "New Inputs" but the method does work with "Both" I think I'll leave the project on both, instead of just reverting to "Old".. Was meaning to play around with the new inputs at some point anyway. Might as well update the 1st person controls I made.
     
    chemicalcrux likes this.
  8. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    No problem! This confused me for a good long while during a game jam (when you don't have a good long while to spare :p )

    I believe this component is used for the new input system:

    upload_2023-1-13_13-3-27.png
     
    IRNoob likes this.
  9. IRNoob

    IRNoob

    Joined:
    Jan 26, 2022
    Posts:
    9
    Awesome! yeah i'll have to try playing with the new one. Always forward as it were! This is definitely my first attempt at trying to gain a better, more useful understanding of creating / applying my own UI. So far most of my focus has been on game mechanics and level design. But got to get on that UI train. It's a super important component for the kinds of games that I'd like to work my way up to creating.

    Thanks again @chemicalcrux! Have a great weekend!