Get an array of components.

Hi everyone! I have a quick problem:

  • I have a GameObject array,
  • I want another array containing one of the component of the corresponding GO (GameObject<em> contains Component<em>, and it’s not mixed_)
    a way to do this? I thought of doing it with a for loop? Maybe there is something simpler…?
    Thank you in advance, Thomas!_

A for loop with GetComponent would work to populate the new array.

Yes, okay!
But…How though? ^^’

Rigidbody[] myComponentArray = new Rigidbody[myGameObjectArray.Length];

for (int i = 0; i < myGameObjectArray.Length; i++) {
  myComponentArray[i] = myGameObjectArray[i].GetComponent<Rigidbody>();
}

I used Rigidbody as an example but replace that with whatever component type you’re interested in.

Okay, perfect! Thank you so much!