- Home /
Update a parent owned Script from within a child script
So I want to make it so that I can update the Parent's Target Script based on 2 Child scripts.
Basically I want it so that if they are within the First Sphere Collider, the OnTriggerEnter event fires and updates the Target to point at the Hero.
I have a Child script that just has an OnTriggerEnter() where it checks if the object is a Hero and then if it is, change the Target to go to it.
I've attached this script to the Enemy object, so the Enemy is the Parent, the Child is this script.
The OnTriggerEnter part of the script just doesn't seem to get called ever, can anyone tell me what I'm doing wrong here?
void OnTriggerEnter(Collider other) {
Debug.Log("Has Entered OnTriggerEnter");
if(other.gameObject.layer == LayerMask.NameToLayer("Hero"))
{
transform.parent.gameObject.GetComponent<SteerForTarget>().Target = other.gameObject.transform;
}
}
Enemy and Hero both have Rigidbody's the GameObject that is attached to Enemy just has a Sphere Collider with the OnTrigger box ticked and my Aggro code, which you see below.
Also i'm using Unity Steer to move the characters around.
Thanks!
Have you checked your collider's "isTrigger" field?
http://unity3d.com/support/documentation/Components/class-SphereCollider.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612185602im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make a simple tree 1 Answer
Becoming parent after OnTriggerStay/Enter? 1 Answer
OnTriggerEnter not being called with multiple colliders 1 Answer
Detection OnTriggerEnter on the Child in Parent script. 2 Answers
Parenting On Trigger not Working? 3 Answers