- Home /
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.
Answer by RAJ_VH · Jan 15, 2013 at 07:47 PM
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.
Answer by harko12 · Jan 14, 2013 at 06:59 PM
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 = "";
}
Tough to tell why without any code to look at. You can try the example the guy posted, below, but it's essentially the same thing I posted.
Try useing this script if you want though its more geared toward useing a texture as opposed to text (it can also be used for note type things you'll see)the note is your texture.
pragma strict
private var guiShow : boolean = false;
var note : Texture;
function On$$anonymous$$ouseEnter()//TriggerStay (Col : Collider) { //if(Col.tag == "Player") //{ guiShow = true; //} }
function On$$anonymous$$ouseExit()//TriggerExit (Col : Collider) { //if(Col.tag == "Player") //{ guiShow = false; //} }
function OnGUI() { if(guiShow == true) { GUI.DrawTexture(Rect(Screen.width / 2.25, Screen.height / 2.25, 100, 93), note); } }
Your answer
![](https://koobas.hobune.stream/wayback/20220613090933im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Wiggle Or shake button possible? 2 Answers
OnGUI Script Not Working With Boolean... 1 Answer
OnMouseDown carry out whole function problem 0 Answers
OnMouseDown and GUI function 0 Answers
GUI opacity. 1 Answer