• 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 Chessnutter · Dec 24, 2017 at 01:45 AM · collisionphysicsbeginnerphysics2dchildren

How do I make a collision detector for the parent which effects the children?

Hello!

I am making this space exploration game, where the player flies around trying not to run into asteroids. I have all the asteroids as a child of Enemy.


The problem is that I cannot figure out how to make my code only work with the children of enemy, not other objects in the game.


I would appreciate any help you can give, thank you!


Here is the code I am using right now, and I know why this doesn't work but I cannot find a solution.


public class Collision : MonoBehaviour {

 private void OnCollisionEnter2D(Collision2D collision) {
     Application.LoadLevel("Lose");
 }

}

Comment
Add comment · Show 1
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 Larry-Dietz · Dec 24, 2017 at 06:05 AM 0
Share

Near duplicate of this other question you asked. Your answer is in that question..

https://answers.unity.com/questions/1446176/referencing-a-child.html

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MezFo · Dec 24, 2017 at 06:26 AM

@Chessnutter You need to check what you are colliding with. The game object that you have collided with is stored in "collision". You will need to add a tag called "Enemy" Or whatever you want to call it and give all the astroids objects that tag(done at top of inspector when astriod is selected.). Then you can check if the object stored in collision has that tag. Like this -

 private void OnCollisionEnter2d(Collision2d collision)
     {
     if(collision.gameObject.tag == "Enemy")
           {
                 Application.LoadLevel("Lose");
           }
     }

Hope this makes sense. Please remember to mark my answer as correct if it does :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Why does children position offset on parent collision? 1 Answer

Unity Physics Collision Execution Order Error? 1 Answer

Executing code on touching edges of a collider from the inside 0 Answers

Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer

Trigger is not executed 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges