- Home /
Tooltip and mouse position
Hi all I had actually posted to another answer and mean to start a question of my own, I am very new to scripting I( am a game artist looking to get into the intricacies of scripting and boought unity several years back when you had to purchase an indie license, I am now starting on making a game and I know that my scripting is very basic but I had taken some examples from here to try and create a tooltip that appears at the mouse when you hover over the object, in this case a button. However when the moueover occurs the tooltip is at the bottom of the screen, I learn better if I know what was wrong could anyone explain to me why my code isnt working and help me get it right? Its intimidatying being new to this type of thing and I hope it does not seem like a dumb question...Here is the code
var mousePos: Vector3 = Input.mousePosition;
function OnGUI () { if (GUI.Button (Rect (10, 10, 100, 50), GUIContent("New Game", "Start a New Game"))) {
Application.LoadLevel (1);
}
GUI.Label (Rect (mousePos.x, mousePos.y - 100, 100, 20), GUI.tooltip);
}
Answer by Eric5h5 · Feb 27, 2011 at 12:44 AM
OnGUI code should use Event.current.mousePosition, not Input.mousePosition (using Input results in screen space coords, not GUI coords). You can't just declare it once, you need to get the position every frame, so it has do be done in OnGUI.
Answer by Hei · Feb 27, 2011 at 12:20 AM
You can use this script
var mousePos: Vector2;
function Update () { mousePos = Input.mousePosition; }
function OnGUI () { if (GUI.Button (Rect (10, 10, 100, 50), GUIContent("New Game", "Start a New Game"))) { Application.LoadLevel (1); } GUI.Label (Rect (mousePos.x, mousePos.y -100, 100, 20), GUI.tooltip); }
Answer by Art Condelles · Mar 03, 2011 at 12:32 AM
Hi Erich thanks for your answer I am trying via your advice to complete the script and for aome reason now that I am calling the mousePosition in the OnGUI function it is not showing this is the current code I am using thanks again....
var mousePos: Vector2 ;
function OnGUI () {
mousePos = Event.current.mousePosition;
if (GUI.Button (Rect (10, 10, 100, 50), GUIContent("New Game", "Start a New Game")))
{
Application.LoadLevel (1);
}
GUI.Label (Rect (mousePos.x, mousePos.y - 100, 100, 20), GUI.tooltip);
}
Answer by Art Condelles · Mar 03, 2011 at 12:59 AM
Update I changed the structure a bit Erich thanks to your help its seems to be working, thank you to all who answered I really appreciate it !! PS I tried to login to say your answer helped me but it wont let me login under any of my IDs....weird...
Your answer
Follow this Question
Related Questions
Setting ToolTip to Above Mouse Position 5 Answers
Additional information on mouseover in the inspector 4 Answers
Mouseover using GUI.Tooltip 0 Answers
Mouse position in camera view area 1 Answer
GUI Texture follow the mouse 1 Answer