- Home /
OnMouseEnter not working
I don't understand why this isn't working, I've a cube on the scene with a simple collider, in the OnMouseEnter a simple print("hello") for testing, but when I move the mouse over the 3d object the function isn't called. Am I forgetting something? What the problem could possibly be?
Here's a silly question: Is the script attached to the gameobject?
Already looked at it, seems like all is set up correctly, but it doesn't work
do you have another collider blocking the collider you're trying to come into contact with?
Answer by aliakbo · Feb 22, 2012 at 05:56 PM
The object using OnMouseEnter must have a collider. Check if yours does.
Answer by bernardfrancois · Sep 29, 2013 at 03:03 PM
OnMouseEnter may not work if an other collider is occluding it. If this is the case, either prevent the other collider from occluding (maybe it can be disabled?), or do a manual check to see if the cursor is inside any object behind it:
bool hovered = false;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
foreach(RaycastHit hit in Physics.RaycastAll (ray))
{
if (hit.collider == collider)
{
hovered = true;
break;
}
}
Answer by James Tima · Feb 22, 2012 at 04:05 PM
where u attached script (where u make function OnMouseEnter )on cube or not, if u attached on cube then it will work perfectly.
Answer by Vampire543 · Oct 18, 2014 at 09:13 PM
if you don't want to use any ray casting, you can do this :
var clicked = false;
function OnMouseDown(){
//click object multiple times to turn on or off.
//if mouse is being clicked else where, then noting should happen.
clicked = !clicked;
}
function Update(){
if (clicked == true){
//what will happen when this is true
}
}
just keep in mind-
1.The object needs a collider
2.This script needs to go on the object you want to be clicking
Answer by MorphVGX · Aug 07, 2017 at 05:41 PM
using UnityEngine;
public class Interactable : MonoBehaviour
{
[SerializeField] private Color _highlightedColor;
private Color _originalColor;
private Material _material;
void Start()
{
Renderer rend = GetComponent<Renderer>();
_material = new Material (rend.material);
rend.material = _material;
_originalColor = _material.color;
}
void OnMouseEnter()
{
_material.color = _highlightedColor;
}
void OnMouseExit()
{
_material.color = _originalColor;
}
}
You better create your own copy of the material so when you change it, other objects aren't affected.
Your answer
Follow this Question
Related Questions
The OnMouseOver and OnMouseEnter functions cease to work when screen.lockcursor = true 0 Answers
Do OnMouse functions work on child colliders? 0 Answers
MouseOver Clarification Please 2 Answers
Collider sometimes fails to detect clicks 0 Answers
Find out if mouseposition is over gui-element or collider? 2 Answers