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
0
Question by ExpiredIndexCard · Apr 27, 2013 at 11:37 PM · mouseclickdownovercover

How to check if mouse over object without adding so many scripts?

I need to check if my mouse is over an object. The problem is that there are a ton of those same objects. Like A LOT of those objects. Do I have to add a script to every object that contains void OnMouseDown() or can I do something inside my main player script? I really need help with this and I cannot find an answer online because it hasnt been asked probably. If I do have to add a script to all those objects, will it slow down my game at all? Thanks and all tips are appreciated.

(If you want more information on what I am doing, I am trying to make a cover system in my game. Whenever my mouse is over an object, my player will move to cover in that specific cover area.) Thanks guys!!

Comment
Add comment
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

2 Replies

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

Answer by proandrius · Apr 27, 2013 at 11:39 PM

Use RayCast instead, it will require only one script.

For example:

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 1000)) {
          Debug.Log("You hit: "+hit.collider.gameObject);
     }
Comment
Add comment · Show 7 · 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 ExpiredIndexCard · Apr 27, 2013 at 11:40 PM 0
Share

Is this going to check whether my mouse is over an object or not? How can I check it using tags? The tag is called "cover" thanks for the quick response

avatar image proandrius · Apr 27, 2013 at 11:42 PM 0
Share

Yes, after "if", you can write, for example: if(hit.collider.gameObject.tag=="cover") { Debug.Log("your mouse is hovering: "+hit.collider.gameObject); }

avatar image ExpiredIndexCard · Apr 27, 2013 at 11:45 PM 0
Share

Wow, I never knew you can use a ray for the mouse position. This helps soooo much! Now I can not only make a cover system, but an interaction system! This is really well done thanks.

avatar image proandrius · Apr 27, 2013 at 11:45 PM 0
Share

No problem. Don't forget to use this in Update function.

avatar image ExpiredIndexCard · Apr 27, 2013 at 11:51 PM 0
Share

Quick question sorry hahaah. I convereted this to C# and I get these errors

UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Ins$$anonymous$$d move initialization to the Awake or Start function. $$anonymous$$ainPlayerScript..ctor ()

Assets/C# Scripts/$$anonymous$$ainPlayerScript.cs(25,35): error CS0165: Use of unassigned local variable hit' Assets/C# Scripts/$$anonymous$$ainPlayerScript.cs(25,21): error CS1502: The best overloaded method match for UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit, float)' has some invalid arguments

Assets/C# Scripts/$$anonymous$$ainPlayerScript.cs(25,21): error CS1620: Argument #2' is missing out' modifier

And here is the code I converted it to:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
 
         if (Physics.Raycast (ray, hit, 1000f)) {
         }
Show more comments
avatar image
2

Answer by Krajca · May 06, 2014 at 05:26 PM

change if statement: if (Physics.Raycast (ray, out hit, 1000f))

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

13 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

Related Questions

Mouse Over & Mouse Click 1 Answer

Problem with OnMouseDown 1 Answer

OnMouseDown() Doesn't work 17 Answers

Prevents a mouse click on interface to trigger a click to move script 1 Answer

Coroutine Chain of Events 1 Answer


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