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
0
Question by pabloj100000 · Feb 27, 2015 at 11:06 AM · scripting problemrigidbody2dparentchild

Clicking on child executes parent script when Rigidbody2D present?

Hi

I have two gameObjects with Box Colliders set to trigger. Each gameObject has its own script, basically just this:

 void OnMouseDown() {
         Debug.Log (gameObject.name);
     }

when I click on the objects it prints the name of the object being clicked. Since I want both to move together I put one object as a child of the other and still I get the correct object name when clicking the parent or the child. However, once I add a Rigidbody2d component to the parent, clicking the child prints the parent name. Is this the way it is supposed to work? If so, how do i probe for which gameObject was actually pressed?

Thanks

Comment
Add comment · Show 3
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 Haxxxxx · Feb 27, 2015 at 11:50 PM 0
Share

Can you post a picture of exactly what it is you're clicking with their colliders showing please.

avatar image cdrandin · Feb 28, 2015 at 12:23 AM 0
Share

Well if there are 2 colliders, 1 being on the parent and child, then it makes sense that both will get executed. Perhaps, you should have something deter$$anonymous$$e which one to exactly use or temporarily disable the parent rigidbody.

avatar image hexagonius · Feb 28, 2015 at 08:19 AM 0
Share

You could also try to replace On$$anonymous$$ouseDown with an Input check in Update, raycast at that point and get the script of the object you casted against. It will return the actual collider, not the parent or something.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Feb 28, 2015 at 09:46 AM

This is normal behaviour. All child colliders are considered to be part of the rigidbody they are parented too. Events like OnTriggerXXX, OnCollisionXXX and OnMouseXXX will be transmitted to the rigidbody.

Another side effect of this is that rigidbodys cannot be parented to other rigidbodies. Try it one day and see what happens. The best way to manage rigidbodys that must move together is to use joints.

Edit: Just checked. The new EventSystem, that is designed to replace the OnMouseXXX methods, does not behave this way. You can use the event system by

  • Adding a physics raycaster to your camera

  • Creating an EventSystem game object (Create -> UI -> EventSystem)

  • Adding using UnityEngine.EventSystem to the top of your script

  • Implementing the IPointerClickHandler interface

Fairly straight forward :)

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

Answer by pabloj100000 · Feb 28, 2015 at 05:01 PM

Thank you all for the replies. I'm really new to Unity and I have to think about your suggestions and ideas are great. I have to think and read more about them.

I ended up fixing the problem using a ray down from the camera and moving the child gameObjects to a newly defined layer (8 in the code below)

 RaycastHit2D hit = Physics2D.GetRayIntersection (Camera.main.ScreenPointToRay(Input.mousePosition), Mathf.Infinity, 1 << 8);

Thanks again Pablo

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Child cant apply variables from parent 2 Answers

Parent class cannot find component 0 Answers

Inheriting Parent Rotation and position 2 Answers

setParent either shrinks my child object or blows it up... What gives? 3 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