hittest always returns null

Hi guys,

Im trying to detect mouse overs on some gui elements. As I understand it I should use GUILayer.HitTest(). Im using:

using UnityEngine;

class CodeFile1 : MonoBehaviour
{
    public void OnGUI()
    {
        GUI.BeginGroup(new Rect(0, 0, 200, 200));
        GUI.Box(new Rect(10, 10, 100, 100), "lalala");
        GUI.EndGroup();
    }

    void Update()
    {
        GUILayer l = (GUILayer)FindObjectOfType(typeof(GUILayer));
        GUIElement e = l.HitTest(Input.mousePosition);
        if (e != null) Debug.Log(e.name); 
    }
}

HitTest() always return null. Any gotcha’s I should know about?

Thanks, Bas

It’s because you’re mixing incompatible GUI systems.

It’s probably not made clear enough in the documentation, but there are two different, unrelated GUI systems in Unity: the old style GUI (made up of the classes GUIElement, GUITexture, GUIText, GUILayer), and the new one (referred to as “UnityGUI” and made up of all the other GUIAnything classes, and the OnGUI() call). So if you’re using UnityGUI, HitTest won’t find anything.

aha, and how would I go about detecting wether the mouse is over a certain gui “thing” in the new gui system?

Note that I need to execute some code, not just change the appearence of the element in question