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 choijaeyoung · Apr 28, 2020 at 12:31 AM · scripting problemtriggercollider2dparent-childparent transform

Can child game object with OnTriggerEnter function use parent game object's collider?

The parent object has Rigidbody2D component, CircleCollider2D component, and a script to project it forward on each update.

The child object attached to the parent object is an empty game object with a script, which has a OnTriggerEnter2D function in it.

     public void OnTriggerEnter2D (Collider2D collision)
     {
         if (collision.tag == "Enemy")
         {
             Debug.Log("Let's do this!");
         }
     }

Obviously, when the parent object collides with (or triggers with) the game object with the tag "Enemy", nothing will be called. I assume that's because child Object searches for the rigidbody and collider components in its own game object, and find nothings.

...

I tried to assign the collider by:

     public Rigidbody2D modifierRigidbody;
     public Collider2D modifierCollider;
 public void Awake()
 {
     modifierRigidbody = GetComponentInParent<Rigidbody2D>();
     modifierCollider = GetComponentInParent<Collider2D>();
 }

Which did not work.

So, the question would be, is there any way you can let the child game object with a script to use / borrow the collider specified in the parent object?

Due to the design of the game, this parent-child game object relationship cannot change. It is necessary for the child object's script to have OnTriggerEnter2D function, not on the parent's game object's script.

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
1
Best Answer

Answer by icehex · Apr 28, 2020 at 01:25 AM

You could create a function in the child's script that is called by the parent's script when the parent observes OnTriggerEnter2D. The parent would need to be enabled as isTrigger.

 // This is the script attached to the parent
 public class ParentTrigger: MonoBehaviour
 {
     private void OnTriggerEnter2D(Collider2D col)
     {
         Debug.Log("A collision has occured in parent " + col);
 
         ParentTriggerDetect child_script = this.GetComponentInChildren<ParentTriggerDetect>();
         child_script .ParentTriggered(col);
     }
 }

 // This is the script attached to the empty child GameObject
 public class ParentTriggerDetect : MonoBehaviour
 {
     public void ParentTriggered(Collider2D col)
     {
         Debug.Log("Child has observed the parent's triggering" + col);
     }
 }
Comment
Add comment · Show 1 · 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 choijaeyoung · Apr 28, 2020 at 02:00 AM 0
Share

Thanks for the answer. That's what I ended up using. Hmm... I wanted to use various Unity physics functions (such as OnTriggerEnter, Stay, Exit, etc...) on the child object, but for now I think the parent object script relaying those trigger events onto child object's script is the workaround for my issue.

If anyone else know there's a way for the child object to use OnTriggerEnter using parent's collider, please let us know.

avatar image
1

Answer by yummy81 · Apr 28, 2020 at 06:17 AM

You can use the observer pattern. For example with delegates.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ParentScript : MonoBehaviour
 {
     public event System.Action action;
     
     void OnTriggerEnter2D(Collider2D collider) => action?.Invoke();
 }


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ChildScript : MonoBehaviour
 {
     ParentScript parentScript;
 
     void Awake() => parentScript = transform.parent.GetComponent<ParentScript>();
     
     void OnEnable() => parentScript.action += Foo;
 
     void OnDisable() => parentScript.action -= Foo;
 
     void Foo() => print(this);
 }
 
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

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

Unity Object Aligment in Different Resolutions and Scales 0 Answers

Detect onMouseDown() on children colliders of a rigidbody parent 2 Answers

Turning off trigger with multiple box colliders? 0 Answers

Animator Trigger Not Working 1 Answer

Script Help -- OnCollisionEnter2D 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