Version: 2021.1
LanguageEnglish
  • C#

GameObject.GetComponentsInParent

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Declaration

public Component[] GetComponentsInParent(Type type, bool includeInactive = false);

Parameters

type The type of Component to retrieve.
includeInactive Should inactive Components be included in the found set?

Description

Returns all components of Type type in the GameObject or any of its parents.

The search for components is carried out recursively on parent objects, so it includes parents of parents, and so on.
Note: If the type you request is a derivative of MonoBehaviour and the associated script can not be loaded then this function will return `null` for that component.

using UnityEngine;

public class GetComponentsInParentExample : MonoBehaviour { void Start() { Component[] hingeJoints;

hingeJoints = GetComponentsInParent(typeof(HingeJoint));

if (hingeJoints != null) { foreach (HingeJoint joint in hingeJoints) joint.useSpring = false; } else { // Try again, looking for inactive GameObjects Component[] hingesInactive = GetComponentsInParent(typeof(HingeJoint), true);

foreach (HingeJoint joint in hingesInactive) joint.useSpring = false; } } }

Declaration

public T[] GetComponentsInParent();

Declaration

public T[] GetComponentsInParent(bool includeInactive);

Parameters

includeInactive Determines whether to include inactive components in the found set.

Description

Generic version of this method.

Note: If the type you request is a derivative of MonoBehaviour and the associated script can not be loaded then this function will return `null` for that component.

using UnityEngine;

public class GetComponentsInParentExample : MonoBehaviour { void Start() { HingeJoint[] hingeJoints;

hingeJoints = GetComponentsInParent<HingeJoint>();

if (hingeJoints != null) { foreach (HingeJoint joint in hingeJoints) joint.useSpring = false; } else { // Try again, looking for inactive GameObjects HingeJoint[] hingesInactive = GetComponentsInParent<HingeJoint>(true);

foreach (HingeJoint joint in hingesInactive) joint.useSpring = false; } } }

Declaration

public void GetComponentsInParent(bool includeInactive, List<T> results);

Parameters

includeInactive Should inactive Components be included in the found set?
results List holding the found Components.

Description

Find Components in GameObject or parents, and return them in List results.

Note: If the type you request is a derivative of MonoBehaviour and the associated script can not be loaded then this function will return `null` for that component.

using UnityEngine;
using System.Collections.Generic;

public class GetComponentsInParentExample : MonoBehaviour { void Start() { List<HingeJoint> hingeJoints = new List<HingeJoint>();

GetComponentsInParent<HingeJoint>(false, hingeJoints);

if (hingeJoints != null) { foreach (HingeJoint joint in hingeJoints) joint.useSpring = false; } else { // Try again, looking for inactive GameObjects List<HingeJoint> hingesInactive = new List<HingeJoint>();

GetComponentsInParent<HingeJoint>(true, hingesInactive);

foreach (HingeJoint joint in hingesInactive) joint.useSpring = false; } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961