- Home /
onmouseover not work
Hello people! Im from Ukraine and i not good speak to Englesh(sorry).I have object consists of different parts, for example there is a mouse object that consists of a wheel, left click, right click, etc. I need that if I were brought to the example on the left mouse click in the console brought the "left click". Trying to each such object (left click, right ...) hang event, but it does not work, the message in the console is not displayed here is the script:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void OnMouseOver() {
print("object name");
}
}
Answer by robertbu · Jan 09, 2014 at 09:56 PM
Your code is fine. Make sure 1) this script is attached to every object you want to report, 2) that every object has a collider, and 3) that the collider you click on is the front-most collider (i.e. there is no hidden collider between the mouse and your objects. You can replace your print() with:
Debug.Log ("Name: "+gameObject.name);
Answer by mihaa123 · Jun 12, 2016 at 02:19 PM
Not sure if you found what was wrong but for other people who had the same problem-
APPARENTLY, a rigidbody component on the parent of the object, without the object itself having a rigidbody is causing these functions not to work-
OnMouseOver(), OnMouseDown(), OnMouseEnter(), OnMouseExit() *maybe there are more, but these are the ones i tested.
I have NO idea why this is happening.
So the fix is either to attach a rigid body on the object on which you are calling the function above OR removing the rigid body component from the parent\parents of this object.
Thanks bud, this was my issue.
It seems the documentation needs a bit of an update on this issue and the inspector does too.
@mihaa123 This is actually the way ALL collision events happen in Unity. If you have 1 object with a rigidbody, and that object has children with colliders, all collisions report to the parent object with the rigidbody. In fact, if you use a RaycastHit, and access its transform, it will always return the object with the rigidbody that the collider belongs to. You have to specifically use the RaycastHit.collider to ensure you are accessing the correct collider.
Answer by moment_um · Jul 19, 2018 at 11:04 PM
Your code is fine. Make sure:
this script is attached to the object you're trying to use
that the object has a collider
Your object is not obstructed (it is the closest thing to the mouse, the event only gets called on the first thing the mouse touches, use Debug.Log ("over: "+ name) if not sure )
it CAN NOT be a UI element (i.e. in the canvas with a rect transform)
also you are probably looking for OnMouseEnter or OnMouseDown as OnMouseOver gets called every frame.
Answer by salkony90 · Jan 12 at 02:45 PM
The thing I found out is that OnMouseOver only works in the Game tab and not working in the Scene tab :)
Your answer
