• 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
Question by PolymathicIndustries · Feb 05, 2022 at 06:12 PM · onmousedowncomparetag

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 t$$anonymous$$s 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 somet$$anonymous$$ng like the following actually work?

 void OnMouseDown () 
      {
          if (gameObject.tag == "objectBlue")
          {
              picked += 1;
              Debug.Log ("tag");
          }
  
      }

I want to use t$$anonymous$$s 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.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by MerryLeo · Feb 05, 2022 at 07:11 PM

Hello @Polymat$$anonymous$$cIndustries, 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 t$$anonymous$$s 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 somet$$anonymous$$ng 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 somet$$anonymous$$ng like the following actually work?

I am assuming you put the OnMouseDown() inside a Script that is on the GameObject itself. In t$$anonymous$$s case, your script should work. Also, I do not t$$anonymous$$nk the CompareTag() method needs a collider, but the OnMouseDown() does.


(3) - I want to use t$$anonymous$$s 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 t$$anonymous$$s helps!

Comment
alexianphilosophy

People who like this

1 Show 3 · 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 PolymathicIndustries · Feb 06, 2022 at 05:45 PM 0
Share

@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?

avatar image MerryLeo PolymathicIndustries · Feb 06, 2022 at 08:16 PM 0
Share

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.

avatar image PolymathicIndustries MerryLeo · Apr 02, 2022 at 09:07 PM 0
Share

@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!

avatar image

Answer by PolymathicIndustries · Jun 11, 2022 at 09:55 PM

@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 $$anonymous$$tInfo;
             if (Physics.Raycast(ray, out $$anonymous$$tInfo))
             {
                 if ($$anonymous$$tInfo.collider.gameObject.tag == "Green Crane")
                 {
                     Debug.Log("We are green!");
 
                 }
             }
         }
Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

OnMouseDown with 360 controller unity? 1 Answer

GameObject Array 1 Answer

Increase mouseclick area size for OnMouseDown() - For Mouse Shooting Game (Open to more ideas) 2 Answers

Click on a gameObject to show/hide a GUI box help C# 1 Answer

Collider priority with onMouseDown() 2 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