How get transform with GetComponents c#

Hi everyone,

I have this variable:

public Transform[] points;

now I want to get the Transform for each GameObject I attached in the Inspector, and I want to do that using GetComponents, I think.

How can I do that properly?

I’ve tried something like that:

void Start()
{
points = points[points.Length].GetComponents<Transform>();
}

It doesn’t work, obviusly :confused:

Thanks and sorry for my English, I’m trying to learn it by myself.

As i mentioned in my comment GetComponents will only search for components on the current gameobject. Since you want the Transform components of your child gameobjects you can’t use GetComponents.

There’s the method [GetComponentsInChildren][1] which basically does what you want. However it also returns components on the actual gameobject as well as it’s children. So if you have one empty gameobject with the script and 5 child gameobjects, GetComponentsInChildren<Transform>() will return 6 Transform components, the 5 child Transforms as well as the Transform of the empty GO.

If you only need the child objects there are several solutions:

  • Use “Linq” to filter out the first element which is the Transform of the empty GO.
  • create and fill the array manually using [transform.childCount][2] and [transform.GetChild][3]

Here’s some sample code:

// Linq solution:
using System.Linq;

// [ ... ]
void Start()
{
    points = GetComponentsInChildren<Transform>().Skip(1).ToArray();
}

//-------------------------------------

// using childCount and GetChild
void Start()
{
    // create new array with "childCount" elements
    points = new Transform[transform.childCount];
    // iterate through all children of this gameobject
    for(int i = 0; i < points.Length; i++)
        points *= transform.GetChild(i);*

}
[1]: Unity - Scripting API: Component.GetComponentsInChildren
[2]: Unity - Scripting API: Transform.childCount
[3]: Unity - Scripting API: Transform.GetChild

Hi @ppddgg

Please clarify, is it situation (A) or (B):
Are you looking to (A) manipulate the position of child Gameobjects under the GameObject that has your component?

Or, are you looking to (B) have one GameObject manipulate a number, lets say 3, other GameObjects in your scene?

If (A) then @Bunny83 has given two great answers!
If (B) then a simpler thing can be done and you weren’t too far off!

If your GameObject needs the ability to manipulate other GameObjects in the scene.
You can create a variable that is an array and the Inspector will let you decide how big the array should be and then drag those objects (or prefabs) into the GameObject slots of your array.

Also, if you’re moving things around in the scene and physics and collision are important, it’s a great idea to make sure each GameObject has a RigidBody component to avoid the physics system updating the cache of static objects all the time. GameObjects without a RigidBody but with a Collider (say MeshCollider, BoxCollider etc) are expected to be static, immovable obstacles.

Here’s an example (warning: I just banged this out directly - not guaranteed to compile but you can reply if you get stuck):

public class FooBar : MonoBehaviour {
  public GameObject[] ObjectsToMoveAbout = null;

  private Transform[] TransformsToMove;

  void Start() {
    SetupTransforms();
  }

  void Update() {
    if (TransformsToMove != null) {
      MoveAssociatedObjects();
    }
  }

  private void MoveAssociatedObjects() {
    // Your fancy movement logic goes here :-)
  }

  private void SetupTransforms() {
    if (ObjectsToMoveAbout == null) {
      TransformsToMove = null;
      Debug.LogFormat("Hey!! Please setup some objects to move for GameObject {0}", gameObject.name);
    } else {
      TransformsToMove = new Transform[ObjectsToMoveAbout.Length];
      int index = 0;
      foreach(GameObject gObject in ObjectsToMoveAbout) {
        TransformsToMove[index] = gObject.GetComponent<Transform>();
        index++;
      }
    }
  }
}

After Start() is called, TransformsToMove will either be a properly setup array with things to move or null because you didn’t set the objects up in your scene and you should see a message in the Console when you forget to do it. :slight_smile: