OnMouseOverGUI

I am not sure if this should be in the Scripting forum but since it has a lot to do with the UI, I have posted it here instead.

Anyway what I am trying to do is have 2 scripts return an item when I mouse over an object.

First of all I have this:

using UnityEngine;
using System.Collections;

public class ItemControl : MonoBehaviour {

    public void OnMouseOver(){
        transform.parent.parent.GetComponent<Inventory>().selectedItem = this.transform;
    }

    void OnMouseExit(){
        transform.parent.parent.GetComponent<Inventory>().selectedItem = null;
    }
}

and then just a public transforms so I can see the item

using UnityEngine;
using System.Collections;

public class Inventory : MonoBehaviour {

    public Transform selectedItem;
}

The problem: the scripts do not return the item. If anyone know how to fix this that would be awesome.

what are you “mousing” over? UI elements or gameobjects in the scene?

if it’s gameobjects in the scene then those gameobjects need colliders and to be in a layer that can be hit with raycasts.

if it’s UI elements, OnMouse###() isn’t going to work. Best approach imo would be to look into the IPointerEnterHandler interface (and the other EventSystems interfaces).

I am “mousing” over UI elements. Thanks yea I thought that because that works for gameobjects that it might work for UI.

Thanks for the help I will try with EventSystems now :slight_smile: