GetMouseButton problems

OK, 3.4 is a bear…

Simple test, while trying to work on an inventory system. I wanted to right click on an object in the screen and add it to my inventory, but low and behold when I try to right click on something… NOTHING HAPPENS

So I built a test application… testing left, middle and right clicks… I found out… Unity no longer registers Middle and Right clicks. (nor does it register Fire2 and Fire3)

Whats up with this, is this a bug, or a feature that they added to jerk people around?

var text : String="";

function OnMouseDown() 
{
	//text="Left Click-" + Input.GetMouseButton(0) + "\n";
	//text+="Middle Click-" + Input.GetMouseButton(2)+"\n";
	//text+="Right Click-" + Input.GetMouseButton(1);
	
	text="Left Click-" + Input.GetButton("Fire1") + "\n";
	text+="Middle Click-" + Input.GetButton("Fire3")+"\n";
	text+="Right Click-" + Input.GetButton("Fire2");
}

function OnGUI(){
	GUI.Label (Rect (10, 10, 160, 80), text);
}

I can’t try your test case right now, but the below works in 3.4

function Update()
{
    if(Input.GetMouseButton(0))
    {
         Debug.Log("left");
    }
    if(Input.GetMouseButton(1))
    {
         Debug.Log("right");
    }
    if(Input.GetMouseButton(2))
    {
         Debug.Log("middle");
    }
}

You sure you are not just missing the blip of the text changing?

The code you pasted will set the text correctly for the frame when you press the button down while hovered over the collider of the game object to which the script is attached. If miss the collider, the text will not be updated and the frame after the one where the text was set, it will be reset again.

heh, this only adds more to the confusion.

Why would OnMouseDown and all those events ONLY recognize mouse button 0?

The code I put sets the text in the frame OnMouseDown… thus will not erase until I have pressed OnMouseDown again. So no I have checked that part.

Ah right. A bit too quick on the trigger there. It’s possible that OnMouseDown only responds to mouse button 0.

OK… given Ivkoni’s response, and testing…

OnMouseDown and OnMouseUp ONLY refer to the left mouse button… (which makes me sad)

So to get around this you must call functions based on the event from Update…

var text : String="";

function MouseDown() 
{
	var x="Left Down-" + Input.GetMouseButtonDown(0) + "\n";
	x+="Middle Down-" + Input.GetMouseButtonDown(2)+"\n";
	x+="Right Down-" + Input.GetMouseButtonDown(1);
	text=x;
}

function MouseUp(){
	var x="Left Up-" + Input.GetMouseButtonUp(0) + "\n";
	x+="Middle Up-" + Input.GetMouseButtonUp(2)+"\n";
	x+="Right Up-" + Input.GetMouseButtonUp(1);
	text=x;
}

function Update()
{
	if(Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))
		MouseDown();
	if(Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(2))
		MouseUp();
}

function OnGUI(){
	GUI.Label (Rect (10, 10, 160, 80), text);
}

This seems like a horrid way to do it, but if that is the way Unity is… that is the way Unity is.

Functionally that is different though as you did not include the raycast check against the collider. This gives you mouse down and up calls regardless of where the mouse was at the time of the interaction.

This isn’t anything new in 3.4. Instead of Update, use OnMouseOver.

–Eric

Oh, sorry, you are correct, I would use OnMouseOver if I was dealing with an object event, but in dealing with an inventory event, which really doesn’t have an object, I would use Update. :wink:

I think that those messages should really be deprecated. You’ll be in a much better position if you write your own raycaster. For example IOS doesnt support the OnMouse* messages and ultimately you really do not get alot of info about the click otherwise.