Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 lalodoble · Oct 02, 2013 at 08:44 PM · raycastmousemouseover

How do I detect when mouse passes over an object?

I've been loocking for this for a while, but all I've found was OnMouseOver() function. I don't konow why but this doesnt work anymore. Raycast doesnt wok either. I need that when the mouse is over an object, image is showed instead of the normal cursor Please help!

Comment
Add comment · Show 2
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 Fattie · Oct 03, 2013 at 07:57 AM 1
Share

As a new user you $$anonymous$$UST choose an answer and click the "TIC$$anonymous$$" button, thanks.

avatar image idiot333 · Jan 28, 2015 at 10:32 PM 0
Share

i have a box collide but on$$anonymous$$ouseOver still doesn't work

3 Replies

· Add your reply
  • Sort: 
avatar image
26
Best Answer

Answer by clunk47 · Oct 02, 2013 at 09:36 PM

Be sure the objects you wish to detect have a Collider attached, and use a RayCast, or OnMouseOver.

You didn't specify your language so I'll give a JS and CS example.

JS Example of Raycast:

//Attach this script to your Main Camera, or an Empty GameObject.

 #pragma strict
 
 var ray : Ray;
 var hit : RaycastHit;
 
 function Update () 
 {
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, hit))
     {
         Debug.Log(hit.collider.name);
     }
 }



C# Example on Raycast:

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour
 {
     Ray ray;
     RaycastHit hit;
     
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit))
         {
             print (hit.collider.name);
         }
     }
 }


JS Example on OnMouseOver():

 //Attach this to each object that will need to be detected. This is why I prefer Raycast, becasue with Raycast, you only need one instance of the script.
 
 #pragma strict
 
 function OnMouseOver() 
 {
     print(gameObject.name);
 }



C# Example on OnMouseOver():

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour
 {
     void OnMouseOver()
     {
         print (gameObject.name);
     }
 }
 
Comment
Add comment · Show 5 · 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 lalodoble · Oct 07, 2013 at 05:04 PM 2
Share

Still this one is the best answer, my bug was that I forgot to add a $$anonymous$$esh Collider to my object, from the Inspector

avatar image MagicalBilly · Jul 10, 2015 at 07:41 PM 0
Share
  using UnityEngine;
  using System.Collections;
  
  public class Example : $$anonymous$$onoBehaviour
  {
      Ray ray;
      RaycastHit hit;
      
      void Update()
      {
          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          if(Physics.Raycast(ray, out hit))
          {
              print (hit.collider.name);
          }
      }
  }

I get a NullReferenceException: Object reference not set to an instance of an object On$$anonymous$$ouseOverTile.Update () (at Assets/Scripts/On$$anonymous$$ouseOverTile.cs:10)

avatar image clunk47 · Jul 10, 2015 at 08:01 PM 0
Share

$$anonymous$$ake sure your camera is tagged $$anonymous$$ainCamera. Do a null check as well.

 using UnityEngine;
 using System.Collections;
 
 Camera mainCam {get {return Camera.main.GetComponent<Camera>();} }
 bool   mainCamFound {get {return mainCam != null;} }
 Ray {get {return mainCam.ScreenPointToRay(Input.$$anonymous$$ousePosition);} }
 RaycastHit hit;
 bool didHitSomething {get {return Physics.Raycast(ray, out hit);} }
 bool hitCollider {get {return hit.collider != null;} }
 
 void Update()
 {
     if(!mainCamFound)
     {
         Debug.LogWarning("$$anonymous$$ain Camera not found!");
         return;
     }
 
     if(didHitSomething && hitCollider)
     {
         Debug.Log("Hit - " + hit.collider.name);
     }
 }
avatar image Nitro00 · Jan 16, 2018 at 05:34 AM 0
Share

The C# example of On$$anonymous$$ouseOver(); does not give me the object that I was looking at but rather the object I attached the script to. Any ideas?

avatar image megabrobro · Dec 14, 2018 at 10:17 PM 0
Share

this guy (clunk47) deserves a nobel prize

avatar image
3

Answer by Sisso · Oct 02, 2013 at 09:22 PM

These links have everything that you need:

http://docs.unity3d.com/Documentation/ScriptReference/Collider.html

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

http://docs.unity3d.com/Documentation/ScriptReference/Input.html

http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html

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 Ash-Blue · Jan 22, 2020 at 08:23 PM

There are 3 options to solve this problem. All of them will trigger the MonoBehavior method OnMouseOver. In my case the issue was trying to detect the mouse via trigger area.

  1. Use a collider (set to trigger false)

  2. Use a collider (set to trigger true). You will need to set your physics settings to force triggers to be detected by raycasts. This is generally not a good idea as every trigger in your game will now intercept raycasts

  3. Create a new layer called Interactions and set it to interact with nothing on the collision matrix. Create a collider and object as normal with the new layer. The important part here is that you have a script that emits an event on the object so other scripts can make use of the mouse events

In my case option 3 did the trick. I can now have invisible trigger detection areas without affecting any other system. It works for keyboard / mouse and could easily be extended to work with a gamepad. Option 1 and 2 both created a giant mess for me.

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

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

New UI ~ Check for mouse-hover 1 Answer

Raycast Destroys player. 1 Answer

Make the player shoot towards a mouse click 2d Platformer 1 Answer

SideScrolling Mouse Pointer Aim - Help 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