Hi. I'm using a simple script:
private void OnEnable()
{
EventManager.OnMouseOverMenu += OnMouseOverMenuElement;
}
private void OnDisable()
{
EventManager.OnMouseOverMenu -= OnMouseOverMenuElement;
}
private void OnMouseOverMenuElement(Menu _menu, MenuElement _element, int _slot)
{
if (_menu.title == "IconBar" && _element.title == "ActiveItem") //do things
}
My iconbar menus has two timers and a few other elements, when mouse is over the timers only, unity throws this error:
NullReferenceException: Object reference not set to an instance of an object
OnMouseOverActiveItem.OnMouseOverMenuElement (AC.Menu _menu, AC.MenuElement _element, System.Int32 _slot) (at Assets/LanguageAdventures/Scripts/OnMouseOverActiveItem.cs:27)
AC.EventManager.Call_OnMouseOverMenuElement (AC.Menu menu, AC.MenuElement element, System.Int32 slot) (at Assets/AdventureCreator/Scripts/Managers/EventManager.cs:842)
AC.PlayerMenus.UpdateAllMenus () (at Assets/AdventureCreator/Scripts/Controls/PlayerMenus.cs:2214)
AC.StateHandler.Update () (at Assets/AdventureCreator/Scripts/Game engine/StateHandler.cs:245)
When I debug.Log the OnMouseOverMenuElement parameters, _element is Null when hover the timers.
Is there something I need to correct? Thanks.
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
OnMouseOverMenu can be called when hovering over a Menu but no Element - you'll need to include a null-check to ensure it's being called at the right time: