• 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 /
  • Help Room /
avatar image
0
Question by Malt27 · May 03, 2017 at 12:34 PM · 2dmouseclick

OnMouseDown to many objects

Hi there!

I need possible check all objects that hit to mouseDown. For example

 if (Input.GetMouseButtonDown (0)) {
   
 hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition),Vector2.zero);
 
             if(hit.collider != null) {
 
                 if (hit.transform.name == "PlayBt") {
                     Debug.Log ("yeah");
                 }
             }
         }

But if something other gameObject with collider place over this, it is not works. How fix this?

Thanks!

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 PizzaPie · May 03, 2017 at 12:49 PM 0
Share

You want to be able to hit (raycast) the "PlayBt" even if there is an obstacle (some GameObject with collider) in the way?

avatar image Malt27 PizzaPie · May 03, 2017 at 12:50 PM 0
Share

Yes, you right!

2 Replies

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

Answer by PizzaPie · May 03, 2017 at 12:56 PM

 if (Input.GetMouseButtonDown(0))
         {
             RaycastHit2D[] hits;
             hits = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);     //not sure about the directions, but if they worked they good for this too
             if (hits == null)
                 return;
             for (int i = 0; i < hits.Length; i++)
             {
                 if (hits[i].collider.name == "PlayBt")
                 {
                 Debug.Log("yeah!");
                 }
             }

As commented in the code i didn't test the directions so i am not sure if they are right, no 2D project in hand to test it. Keep in mind that the above code has great performance cost. Cheers

Edit: the above method is killing performance so go with this, my bad i didn't mention it before

 if (Input.GetMouseButtonDown(0))
         {
             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, someMask);
             if (hit.collider != null)
             {
                 if(hit.collider.name == "PlayBt")   //no need for this if no other object has the same LayerMask
                 {
                     Debug.Log("yeah!");
                 }
             }

 int someMask;
 //...
 
 void Awake(){
        someMask = LayerMask.GetMask("someMask");
 }

Using Masks the raycast will ignore any object that is not marked with this mask. To set the mask of an Object click on it, on the inspector there is a drop down named Layer on the top click on it go to AddLayer add your Layer (just a name) and then go back to your object and set the Layer, as soon you finish this add the above code and make sure in the "GetMask("someMask")" the someMask matches the name of the Layer you created.You should use this instead of my initial suggestion. Also another way would be to add the IgnoreRaycast layer(mask) on all objects you want to be ignored but i don't like it.

Comment
Add comment · Show 6 · 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 Malt27 · May 03, 2017 at 01:00 PM 0
Share

Easy way, thanks!

avatar image ITZZZETHAN · May 03, 2017 at 01:04 PM 0
Share

hey man think you could help out with my question! That is if you know multiplayer

avatar image Malt27 ITZZZETHAN · May 03, 2017 at 01:04 PM 0
Share

sorry, it is my first day with Unity

avatar image ITZZZETHAN Malt27 · May 03, 2017 at 01:05 PM 0
Share

haha was talking about the other guy xD thanks anyway tho

avatar image PizzaPie ITZZZETHAN · May 03, 2017 at 01:31 PM 0
Share

No clue about multiplayer.

avatar image HighVolt4160 · May 04, 2017 at 10:04 AM 0
Share

Thanks for the information explaining how to do masks. It is what I have been looking for.

avatar image
0

Answer by ITZZZETHAN · May 03, 2017 at 12:37 PM

have you put this in void Update () if not I suggest you do that might fix your problem! also this

               if(hit.collider != null) {
 
              if (hit.transform.name == "PlayBt") {
                  Debug.Log ("yeah");

do this instead:

              if (hit.collider != null) if (hit.transform.name == "PlayBt")
              {
                    Debug.Log("Yeah");
              }

I hope this fixes your issue!

Comment
Add comment · Show 4 · 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 Malt27 · May 03, 2017 at 12:42 PM 0
Share

Sure. It works if nothing covered this gameobj

avatar image ITZZZETHAN Malt27 · May 03, 2017 at 12:43 PM 0
Share

try what I just posted :)

avatar image Malt27 ITZZZETHAN · May 03, 2017 at 12:49 PM 0
Share

no works...

Show more comments

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

147 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 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

How to allow clicks to go through Circle Collider 2D on sprite prefab? 0 Answers

Hide/reveal shader and material to make scene appear on touch event 0 Answers

C# OnCollisionEnter not Triggering If Statement 1 Answer

Mobile screen resolution 0 Answers

Get a constant Speed 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