GUI text appear when mouse hovering on a object?

Hello there,

I seem to have a problem with scripting, so I hope you don’t mind me asking this.

Long story short, I’m making a game. Yeah, it’s simple as that. I’m not good at scripting, I don’t know how to include a function in a function (OnMouseEnter + OnGui,) so excuse me.

Basically, when I hover my mouse on a object, a gameobject that exists in my game, some text appears, and when my mouse is not making out with it, the text is gone.

Is that possible? Can anyone help me with this?

Thanks.

You can use this script:


var text = "YOUR TEXT HERE";

private var currentToolTipText = "";
private var guiStyleFore : GUIStyle;
private var guiStyleBack : GUIStyle;

function Start()
{
    guiStyleFore = new GUIStyle();
    guiStyleFore.normal.textColor = Color.white;  
    guiStyleFore.alignment = TextAnchor.UpperCenter ;
    guiStyleFore.wordWrap = true;
    guiStyleBack = new GUIStyle();
    guiStyleBack.normal.textColor = Color.black;  
    guiStyleBack.alignment = TextAnchor.UpperCenter ;
    guiStyleBack.wordWrap = true;
}

function OnMouseEnter ()
{

    currentToolTipText = text;
}

function OnMouseExit ()
{
    currentToolTipText = "";
}

function OnGUI()
{
    if (currentToolTipText != "")
    {
        var x = Event.current.mousePosition.x;
        var y = Event.current.mousePosition.y;
        GUI.Label (Rect (x-149,y+40,300,60), currentToolTipText, guiStyleBack);
        GUI.Label (Rect (x-150,y+40,300,60), currentToolTipText, guiStyleFore);
    }
}

I found the script in the Free Basic FPS Horror Type Package by MadToLove.

I think you might be able to do it by using a member variable for the text you want to display. When you hover on the object, you can set the member variable text to whatever you want, and then when you leave the object, it will set back to “”. for instance

var Message = "";

OnGUI()
{
 GUI.Label(new Rect(10,10,200,30),Message);
}

OnMouseEnter()
{
 Message = "Here I am.";
}

OnMouseExit()
{
 Message = "";
}