- Home /
Multiple object clicks in OnMouseDown script?
I was wondering how you would manage clicking many objects (just like in a escape click game) and whether you would have an individual script for each and every object clicked or whether you could manage this in one script simply with a tag system? I looked up documentation for OnMouseDown and CompareTag: https://docs.unity3d.com/ScriptReference/Component.CompareTag.html https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
CompareTag does not seem to be able to used without a collider so I am not sure how I would check the tag with a mouse click. Would something like the following actually work?
void OnMouseDown ()
{
if (gameObject.tag == "objectBlue")
{
picked += 1;
Debug.Log ("tag");
}
}
I want to use this for either a board game or escape clicking game where I would need dozens of decisions (if statements) to determine what happens when each object is clicked.
Answer by MerryLeo · Feb 05 at 07:11 PM
Hello @PolymathicIndustries, I am quite new to Unity, but I will try to answer as best as I can your questions
(1) - you would have an individual script for each and every object clicked or whether you could manage this in one script simply with a tag system?
Both are possible, but if you wanted to do the second method you would probably need to use Unity Input System instead, because the OnMouseDown() function is called from the GameObject that has been clicked. You would use something like Input.GetMouseButtonDown(int button) and use the mouse position on the screen and a ray to find the GameObject that has been clicked.
(2) - CompareTag does not seem to be able to used without a collider so I am not sure how I would check the tag with a mouse click. Would something like the following actually work?
I am assuming you put the OnMouseDown() inside a Script that is on the GameObject itself. In this case, your script should work. Also, I do not think the CompareTag() method needs a collider, but the OnMouseDown() does.
(3) - I want to use this for either a board game or escape clicking game where I would need dozens of decisions (if statements) to determine what happens when each object is clicked.
I do not know what type of board game you are trying to make, but I'll say if you have a lot of GameObjects in your scene you might want to have one GameObject with a script that manages the interactions. But if you do not have that many gameobjects, using individuals script with the OnMouseDown() function is probably preferable. I hope this helps!
@MerryLeo , thanks for the help! I did not even understand the difference between OnMouseDown() and the Input.GetMouseButtonDown. I'm pretty new to Unity as well and am trying to learn as much as possible. Can you explain how the mouse position and ray find you what object is clicked? I know it must be obvious, but I would appreciate the explanation.
How would you I check the tag off an object if my script is not attached directly to the gameobject?
Hello again @PolymathicIndustries!
I want to say first that I do not think these concepts are easy for someone who's new to Unity, so your questions are perfectly fair.
(1) - Can you explain how the mouse position and ray find you what object is clicked?
In game development, a method that is often used is Ray casting. Basically, in my own word, this method consists of sending an invisible ray that gets useful information for the game such as the object that was hit. Unity has a built-in function just for that called Physics.Raycast. You would use that function to send a ray from the player's camera, then if this ray hits an object it will return true. You would then access the GameObject with the hitInfo optional parameter of the Physics.Raycast function and check the GameObject's tag. Here is a code snippet for you:
void Update()
{
// Player pressed left mouse button
if (Input.GetMouseButton(1))
{
// Throw Ray from camera
float screenX = Input.GetAxis("Mouse X"); // Get Horizontal Mouse Position on the Screen
float screenY = Input.GetAxis("Mouse Y"); // Get Vertical Mouse Position on the Screen
Vector3 mousePos = playerCamera.ScreenToWorldPoint(new Vector3(screenX, screenY, 0)); // 2D Mouse Position to 3D World Mouse Position
Ray ray = new Ray(mousePos, transform.forward); // Ray that points in the camera forward direction
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo)) // Send Ray
{
if (hitInfo.transform.tag == "Dice")
{
// Do stuff here
}
}
}
}
(2) - How would you I check the tag off an object if my script is not attached directly to the gameobject?
I think your first question already answered that one, but you would make an empty GameObject with a script like "ThrowRayFromCamera". Using the Physics.Raycast function, you can get a ton of useful information on the GameObject that has been hit (like its tag). Though remember that the Physics.Raycast will only hit GameObjects with colliders, so you need to have a collider component on the different GameObjects.
Hopefully, I answered your question properly. Feel free to ask me other questions. You can also check this video on selecting GameObjects with Raycasting.
@MerryLeo, sorry for the delay. The above script is much appreciated! With the above ability to click on any object in the screen would I simply make many if statements within the "send ray" portion each with a specific tag and instruction with what to do if the right object is found? Also, Would the sample script above be pleased onto my camera? Thanks again!
Answer by PolymathicIndustries · yesterday
@MerryLeo, can you tell me why the following does not work? I'd appreciate a quick look.
if (Input.GetMouseButtonDown(0))
{
Ray ray = greenCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.collider.gameObject.tag == "Green Crane")
{
Debug.Log("We are green!");
}
}
}