T GetComponentInParent( bool includeDisabled, bool includeSelf )

book_4_sparkGenerated
code_blocksInput

Description

The GetComponentInParent<T> method is used to retrieve a component of type T from the current GameObject or any of its ancestor GameObjects in the scene hierarchy. This method is useful when you need to access a component that might be located on a parent object rather than the current object itself.

Usage

To use this method, specify the type of component you are looking for as the generic type parameter T. You can also specify whether to include disabled components and whether to include the current GameObject in the search.

  • includeDisabled: A bool indicating whether to include components that are currently disabled in the search.
  • includeSelf: A bool indicating whether to include the current GameObject in the search.

Example

// Example of using GetComponentInParent<T>

// Assume we have a GameObject with a parent that has a Rigidbody component
Rigidbody parentRigidbody = gameObject.GetComponentInParent<Rigidbody>(includeDisabled: false, includeSelf: false);

if (parentRigidbody != null)
{
    // Do something with the Rigidbody component
    parentRigidbody.AddForce(Vector3.up * 10);
}