Version: 5.5 (switch to 5.6b)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

GameObject.GetComponentsInChildren

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
public function GetComponentsInChildren(type: Type, includeInactive: bool = false): Component[];
public Component[] GetComponentsInChildren(Type type, bool includeInactive = false);

Parameters

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

Description

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

The search for components is carried out recursively on child objects, so it includes children of children, and so on.

#pragma strict
public class GetComponentsInChildrenExample extends MonoBehaviour {
	function Start() {
		var hingeJoints: Component[];
		hingeJoints = GetComponentsInChildren(HingeJoint);
		if (hingeJoints != null) {
			for (var joint: HingeJoint in hingeJoints)
				joint.useSpring = false;
		}
		else {
			// Try again, looking for inactive GameObjects
			var hingesInactive: Component[] = GetComponentsInChildren(HingeJoint, true);
			for (var joint: HingeJoint in hingesInactive)
				joint.useSpring = false;
		}
	}
}
using UnityEngine;

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

hingeJoints = GetComponentsInChildren( typeof(HingeJoint) );

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

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

public function GetComponentsInChildren(): T[];
public T[] GetComponentsInChildren();
public function GetComponentsInChildren(includeInactive: bool): T[];
public T[] GetComponentsInChildren(bool includeInactive);

Parameters

includeInactive Should inactive GameObjects be included in the found set?

Returns

T[] A list of all found components matching the specified type.

Description

Generic version. See the Generic Functions page for more details.

#pragma strict
public class GetComponentsInChildrenExample extends MonoBehaviour {
	function Start() {
		var hingeJoints: HingeJoint[];
		hingeJoints = GetComponentsInChildren.<HingeJoint>();
		if (hingeJoints != null) {
			for (var joint: HingeJoint in hingeJoints)
				joint.useSpring = false;
		}
		else {
			// Try again, looking for inactive GameObjects
			var hingesInactive: HingeJoint[] = GetComponentsInChildren.<HingeJoint>(true);
			for (var joint: HingeJoint in hingesInactive)
				joint.useSpring = false;
		}
	}
}
using UnityEngine;

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

hingeJoints = GetComponentsInChildren<HingeJoint>( );

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

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

public function GetComponentsInChildren(results: List<T>): void;
public void GetComponentsInChildren(List<T> results);
public function GetComponentsInChildren(includeInactive: bool, results: List<T>): void;
public void GetComponentsInChildren(bool includeInactive, List<T> results);

Parameters

results List to receive found Components.
includeInactive Should inactive GameObjects be included in the found set?

Description

Return all found Components into List results.

#pragma strict
public class GetComponentsInChildrenExample extends MonoBehaviour {
	function Start() {
		var hingeJoints: List.<HingeJoint> = new List.<HingeJoint>();
		GetComponentsInChildren.<HingeJoint>(false, hingeJoints);
		if (hingeJoints != null) {
			for (var joint: HingeJoint in hingeJoints)
				joint.useSpring = false;
		}
		else {
			// Try again, looking for inactive GameObjects
			var hingesInactive: List.<HingeJoint> = new List.<HingeJoint>();
			GetComponentsInChildren.<HingeJoint>(true, hingesInactive);
			for (var joint: HingeJoint in hingesInactive)
				joint.useSpring = false;
		}
	}
}
using UnityEngine;
using System.Collections.Generic;

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

GetComponentsInChildren<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>();

GetComponentsInChildren<HingeJoint>( true, hingesInactive );

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