Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The Unity of small science

As always, here are some popular science tips from Unity:

  • Unity is a real-time 3D interactive content creation and operation platform.
  • All creators, including game development, art, architecture, car design, film and television, use Unity to bring their ideas to life.
  • The Unity platform offers a comprehensive suite of software solutions for creating, operating and monetizing any real-time interactive 2D and 3D content on mobile, tablet, PC, game console, augmented reality and virtual reality devices.
  • You can also simply think of Unity as a game engine that can be used to make professional games!

Learn Unity

Gets all the children of a gameobject

Sometimes in Unity we have the need to take all the children of an object and do something together

We can get the child objects of this object using GetChild, but it’s cumbersome to get them one by one

So here’s a way to get all the children: GetComponentsInChildren

Usage Examples:Attach the script to the scene and assign a gameobject

    public GameObject @object;
    Transform[] transforms;

    void Start()
    {
        // All active sub-objects under the gameobject are retrieved, including the gameobject itself
        //transforms [email protected]<Transform>(true);

        // Active sub-objects under the gameobject are picked up, including the gameobject itself; What's not activated won't be taken
        transforms = @object. GetComponentsInChildren<Transform>(false);

        / / traverse
        foreach (Transform t in transforms)
        {
            // Print the child object
            Debug.Log("The value of t is:+t); }}Copy the code

The print result is as follows:You can code to control whether or not you want to get deactivated child objects, so you can get all child objects in one method!

Here are some common methods by the way

Method of getting the number of children of an object

  a = @object.transform.childCount;
  Debug.Log("The number of child objects is:" + a);
Copy the code

Print result:

Gets the index value of the current object (subclass of the current object, starting from 0)

 int child = transform.GetSiblingIndex();
Copy the code