OnMouseOver Problems

Hey everyone!

I am extreme newbie and I have been doing a ton of tutorials, but I seem stuck in this area.

Basically I am writing a script where when the mouse goes over a specific object it displays text to the user. ( the geometry does have a collider)

var text = "Are you sure?";

function OnMouseOver () 
{
	guiText.text = "Are you sure?";
}

I have a guiText component attached to the object with the text stating Are you sure? The component is enabled.

I then attach the script to the object also. Once I am in the game and my mouse goes over the object nothing displays on the screen and the listener window does not even show that the mouse has gone over the object.

I am really confused and looking for advice so please don’t pound me.

Dave

I remade some tests, here is the results :

  • GUILayer component is required on the camera to display GUI elements (it should be already present).

  • The position of the GUIText is relative to the value transform.position (can be seen and changed in the Inspector, component Transform, Position field). It means, for a position of (0.0, 0.0, 0.0), the GUIText is at the … bottom left of the screen. At (1.0, 1.0, 1.0), it is a the top right.

  • The Z value of the position is the depth (from my memories).

Since I highly doubt your cube position X is between 0.0 AND 1.0, and your cube position Y is between 0.0 and 1.0, it will be outside the screen. :wink:

Thanks for the reply.

So is there a way to display a gui element during a mouse over function?

Thanks in advance.

David

You would make a variable of type GUIText on your cube, make an empty game object with GUIText component, and drag and drop that object in the GUIText variable of the cube.

public var guiTextDisplay : GUIText;

function OnMouseEnter ()
{
	guiTextDisplay.text = "Are you sure ?"
}

function OnMouseExit ()
{
	guiTextDisplay.text = "";
}

Thank you for the reply. The information about the empty game object was great. I had no idea about it.

I will let you know how it works out.

David

Ok. I got the text to pass into the GUIText onMouseOver, but it does not display anywhere in the screen area. The camera does have a guiLayer.

Thanks again for all of your help.

David

Set the Transform position of the empty game object with the GUI Text to (0.5, 0.5, 0.0).

That worked… it put the text right in the center of the screen.

Can I ask as to why the transform had to be those numbers?

The onMouseExit function is not working as of now, the text just remains on the screen after leaving the “box”.

No need to answer on how to fix it though. I am going to try to figure it out. If I start to pull my hair out I may ask for a little guidance.

Sorry for so many questions I am still an extreme beginner. But I do practice and learn every day.

Thanks again,

David

http://unity3d.com/support/documentation/Components/class-GuiText.html

Thank you sir!

David

Ok. Well I am back and have solved the issue.

The code is:

var guiTextDisplay :GUIText;

function Update () 
{
    }
	
function OnMouseEnter () 
{
	guiTextDisplay.text = "Are you sure?";
    }

	
function OnMouseExit () 
{
	guiTextDisplay.text = "";
	
	}

Originally I did not include the function Update.
So without the function Update, the scrip would display the text on the mouse over, then the text would disappear after the mouse exit. The problem was that when the mouse went back over it would not display the text again.

So here is my question.

Is an empty function Update required if you are looking for these results or is there a way to script this without the function Update?

I am still learning but I can tell you taking the time to figure out the last problem on my own, although very simple, was extremely gratifying.

Your help has been invaluable to me as a beginner.

Thanks again for all of your help.

David

To be honest, I don’t think it should matter whether or not you have an empty Update() method. Perhaps this should be forwarded to the Unity team.

Yeah without the function Update added into the code it would not work.

Are you saying that you think the function Update should be inherent unless a different function is called?

I will wait to see if there are any other comments or suggestions.

David

I highly doubt Update is required. Whenever I have a script that don’t use it, I remove it.

function OnMouseEnter ()
{
	guiText.text = "In";
	Debug.Log ("In");
}

function OnMouseExit ()
{
	guiText.text = "Out";
	Debug.Log ("Out");
}

Put that on a unity cube, you will see the messages.

Huh… that wouldn’t work for the longest time. I may have done something wrong.

Anyway I added the GUIText as a var to your script and it works!

I am still curious as to why it refused to work before…

Thanks again sir.

David