• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by sonic220 · Feb 22, 2012 at 09:18 AM · collidermouseonmouseenteronmouse

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?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CHPedersen · Feb 22, 2012 at 11:24 AM 0
Share

Here's a silly question: Is the script attached to the gameobject?

avatar image sonic220 · Feb 22, 2012 at 11:50 AM 0
Share

Yes it is, I really don't know what to do.

avatar image Kacer · Feb 22, 2012 at 12:40 PM 0
Share

http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseEnter.html

avatar image sonic220 · Feb 22, 2012 at 12:50 PM 0
Share

Already looked at it, seems like all is set up correctly, but it doesn't work

avatar image Kacer · Feb 22, 2012 at 12:56 PM 1
Share

do you have another collider blocking the collider you're trying to come into contact with?

6 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by aliakbo · Feb 22, 2012 at 05:56 PM

The object using OnMouseEnter must have a collider. Check if yours does.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[2D] Raycast to mouse cursor = offset on Y axis 0 Answers

How can I capture a mouse position on an invisible plane without a collider? 2 Answers

How can I only instantiate if mouse over? 1 Answer

Why Is OnMouse Not Working Correctly? 1 Answer

GUI flashing on hover 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges