• 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 Seric · May 15, 2014 at 06:04 PM · 2dcollisionparent

Need to get parent Object after collision

I've got same troubles with getting parent Object. My object Monster has a MonsterBody Component. After collision with MonsterBody I need to let my Monster know, that he's been attacked. I've made some research, but it didn't work anyway. I guess, I don't get how parents really works. Here's the code:

if (collider.tag == "MonsterBody") collider.GetComponent(),MonsterBody,.transform.parent.gameObject.GetComponent,Monster,().GetHit(BaseDamage*DamageMOD);

I'll glad if you could help me. I'm not really interested in Scripting GetHit() in my MonsterBody, except is the only way.

EDIT: I used "," instead of "<>", cause it seems not working here.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · May 15, 2014 at 06:08 PM

Okay, well, to start with.... collider.tag is always going to be the tag of the object this script is on. So it will always be true or false.

Thats because "MonoBehaviour.collider" is a short cut for for "MonoBheaviour.gameobject.collider"

I strongly suspect thats not what you want.

Similarly 'collider.GetComponent().transform.parent" is going to be the same as "transform.parent" or again, the parent of the transform of the game object this script is on.

To get the object collided with, and its parent object, you need to do something like this:

 void OnCollisionEnter(Collision collision){
   GameObject other  = collision.gameObject;
   GameObject otherParent = other.transform.parent.gameObject;
 }

Comment
Add comment · Show 2 · 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 Seric · May 15, 2014 at 06:53 PM 0
Share

Ok, big thx man, I solved the problem ! :)

I now understand, that I was trying to get parent from part of gameObject, not just gameObject. Here's the solution(and reason why I was using "tag" ;))

Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 1);

      foreach (Collider2D collider in colliders){
       if (collider.tag == "EnemyBody"){ 
           GameObject body = collider.gameObject;
           GameObject bodyParent = body.transform.parent.gameObject;
           bodyParent.GetComponent<Enemy>().GetHit(BaseDamage*Damage$$anonymous$$OD);
       }
      }
avatar image Jeff-Kesselman · May 15, 2014 at 07:29 PM 0
Share

Glad it helped.

In general its not a good idea to redefine variables that already exist in the environment like "collider". You can do it, and it will work, but it makes the code harder to figure out when you look at it later and thus is not great practice.

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

22 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

Related Questions

2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers

Objects don't collide 1 Answer

Particles and 2D games 1 Answer

OnCollisionEnter2D Questions 0 Answers

Change shape of object upon collision 1 Answer


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