The `GetComponentInParent` function in the `UnityEngine.GameObject` class allows you to retrieve a component of a parent object. This is useful when you need to access a component on a higher level of the hierarchy.
Example 1:
Suppose you have a GameObject called "Player" and it has a Rigidbody component attached to it. Further, the "Player" object is a child of another GameObject called "Environment". You want to access the Rigidbody component of the "Player" object from a script attached to the "Environment" object. You can use GetComponentInParent to retrieve the Rigidbody component of the "Player" object as follows:
Suppose you have a GameObject called "Enemy" and it has a Health component attached to it. Further, the "Enemy" object is a grandchild of the GameObject called "Root". You want to access the Health component of the "Enemy" object from a script attached to the "Root" object. You can use GetComponentInParent to retrieve the Health component of the "Enemy" object as follows:
Health enemyHealth = GetComponentInParent().GetComponentInChildren();
The package library for `GetComponentInParent` is `UnityEngine`.
C# (CSharp) UnityEngine GameObject.GetComponentInParent - 38 examples found. These are the top rated real world C# (CSharp) examples of UnityEngine.GameObject.GetComponentInParent extracted from open source projects. You can rate examples to help us improve the quality of examples.