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
2
Question by Jack-Mariani · Apr 12, 2015 at 10:13 AM · c#buttonmouseclick

OnMouseDown gets both Collider and UI Button

Hi Everyone, I'm making a 2d game, and I'm implementing some buttons on the game screen.

These are the elements on the screen:

  • Game Objects (let's call it "Soldier") in the level, with a sprite rendere and a box2d collider. I'm using a OnMouseDown event to select units on the screen.

  • UI Buttons, that are

The situation I'd like to solve is the following: If I click on the button and there's a "Soldier" under it I also select the Soldier.

I'd like to avoid that. If I click on the button I'd just like to use the button and do not select anything below it.

Comment
Add comment · Show 4
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 Pharaoh_ · Apr 12, 2015 at 10:17 AM 0
Share

Use a layer and raycast to an element of that layer (in your case UI).

avatar image Jack-Mariani · Apr 12, 2015 at 10:21 AM 0
Share

When I click the mouse I can cast a ray and make it stop if it hits the button, but I'd like to try using the On$$anonymous$$ouseDown event. Is it impossible?

avatar image Pharaoh_ · Apr 12, 2015 at 11:33 AM 0
Share

Do you also want to raycast on the Soldier? If not, then switch their layer to "Ignore Raycast". On$$anonymous$$ouseDown works on both UI elements and colliders, this is why you're having the problem.

avatar image Jack-Mariani · Apr 12, 2015 at 11:38 AM 0
Share

I understand the issue of On$$anonymous$$ouseDown. I'd like to know if there's a simple solution, or also a complex one. Otherwise I'll create a raycast and make it stop if it reaches the UI button.

Yes. I want also to "raycast" the Soldier.

If there's only the solider I'd like to select it (using On$$anonymous$$ouseDown). If I've both button and soldier I'd like to just select the button (using the Unity UI).

1 Reply

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

Answer by patryk006 · Apr 22, 2015 at 01:12 PM

Look at this:

https://www.youtube.com/watch?v=EVZiv7DLU6E

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Scripting button functionality inside class with created object's method 1 Answer

C# For loop in button to set gameObjects in array to active 1 Answer

My function is called twice on single click 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