- Home /
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!!
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);
}
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
Yes, after "if", you can write, for example: if(hit.collider.gameObject.tag=="cover") { Debug.Log("your mouse is hovering: "+hit.collider.gameObject); }
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.
No problem. Don't forget to use this in Update function.
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)) {
}
Answer by Krajca · May 06, 2014 at 05:26 PM
change if statement: if (Physics.Raycast (ray, out hit, 1000f))
Your answer
Follow this Question
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