Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 supreethg9 · Dec 31, 2018 at 07:26 PM · triggerunity 2dparent-childontriggerenter2d

My trigger for my parent object gets triggered from child and parent,My parent object gets triggered from the child object

So I've been developing this game in which the player must shoot targets that can fire back. one of the targets is a parent of child object which is a large invisible rectangle with the soul purpose of informing the parent if it has found the player so it can attack. I also put a script in the parent that tells it that if the player successfully shoots at it without missing that it should destroy itself. But for some reason both the parent and the child can trigger this. that means with my parent can be destroyed from too far away.

in this image the blue arrow points to my parent object, the red points to the child which informs the parent if it has found the player or not and the arrow in black points at the player. (the payer can shoot up but when it hits the child object it triggers the parent to destroy itself!)

Code that kills jet upon hit: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class JetKill : MonoBehaviour {
     // Use this for initialization
                                       
         if (collision.CompareTag("playerShoot"))//player shoot is the tag for the players projectile
         {
             Destroy(gameObject);
         }
     
 }

,So I've been developing this game where the player has to shoot targets that can fire back. one of the targets is a parent object with a child paired to it that is a invisible wall meant to detect if the player is in its range. Now when I've scripted my parent object to destroy itself upon detecting the players bullets but it gets triggered even when they hit the child of the parent which I don't wantalt text

In that image the blue arrow points towards my parent object (the jet), the black one points to the player and the red one points towards the player detector. If you need anymore details ill provide as much as possible, thank you!

My parent object code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class JetKill : MonoBehaviour {
     public GameObject BOOM;
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         
     }                                          
     public void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.CompareTag("jetKill")) //Kill jet at end
         {
             Destroy(gameObject);
         }
         if (collision.CompareTag("playerShoot"))
         {
             Instantiate(BOOM, transform.position, Quaternion.identity);
             Destroy(gameObject);
         }
     }
 }

Although applied on parent object it for some reason gets triggered by child

help.png (280.0 kB)
help.png (280.0 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by c_Feng · Dec 31, 2018 at 09:22 PM

Your colliders are being treated as a single (compound) collider. I would place each collider of the enemy in a different layer and have them only interact with the layer(s) the collider is supposed to interact with in your Physics settings.

See these other answers:
https://answers.unity.com/questions/410711/trigger-in-child-object-calls-ontriggerenter-in-pa.html
https://answers.unity.com/questions/810312/get-ontriggerenter-of-a-child.html

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 sean244 · Jan 01, 2019 at 08:26 PM

All you have to do is add a rigidbody to the child and set it to kinematic. Should fix your problem immediately.

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

120 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

Related Questions

Ontriggerstay2d only runs 26 times in a row? 3 Answers

Is There A Way To Disable a Trigger But Still Use OnTriggerExit2D? (For COOP switch management) 2 Answers

OnTriggerEnter2D called twice sometimes when IsTrigger is ON 2 Answers

How to GetComponent once for multiple comparisions 2 Answers

OnTriggerEnter2D not working 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