Skip to content

The GetAllChildren() Method

Narek Torosyan edited this page Jan 6, 2019 · 1 revision

About

This method extends GameObject.

This method returns every GameObject as an array.

This method can be used for any task requiring all children of an object.

Example

// This script spawns 3 children and attaches itself as a parameter to them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NUDev.PlusSharp;

public class Spawner : MonoBehaviour {
    public GameObject pref;
    public int spawnQty = 2;

    void Start() {
        for (int i = 0; i < spawnQty; i++) {
            var go = Instantiate(pref);
            go.transform.parent = transform;
        }
        var allChildren = gameObject.GetAllChildren();
        for (int i = 0; i < allChildren.Length; i++) {
            allChildren[i].GetComponent<Player>().manager = gameObject;
        }
    }
}

Clone this wiki locally