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");
    }
}

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);

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.

Your code is fine. Make sure:

  1. this script is attached to the object you’re trying to use
  2. that the object has a collider
  3. 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 )
  4. 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.

The thing I found out is that OnMouseOver only works in the Game tab and not working in the Scene tab :slight_smile: