My slightly different solution (didn't use GetComponentsInChildren)

public class Player : MonoBehaviour {

public bool reSpawnNow = false;
public Transform spawnPoints;

private void ReSpawn()
{
    Transform spawnPoint = spawnPoints.GetChild(Random.Range(0, spawnPoints.childCount));
    transform.position = spawnPoint.position;
    transform.rotation = spawnPoint.rotation; // include if you want to determine rotation at respawn
}

void Update()
{
    if(reSpawnNow)
    {
        ReSpawn();
        reSpawnNow = false;
    }
}

}

This is a good example which shows the difference of priority assigned during design between memory occupation vs execution speed.

Your code prioritizes memory occupation, since you call every time GetChild() but don’t create the spawn points array, whereas creating an array at start prioritizes execution speed, since it calls the method just N times (where N is the number of spawn points) at the expense of more memory occupation due to the spawn points array.

1 Like

Good point. I had nearly the same code as Clem, and in this case counting the children and addressing a random one (and then getting its transform) isn’t much of a deal in terms of computing power. Although I have now adapted my code to count the number of children on Start.
I guess it is important to consider whether the extra memory used is worth that extra responsiveness. It depends on the target platform in my opinion.

You can also use the built-in iterators on gameobjects or transforms something like this:

List<Transform> spawnPoints = new List<Transform>();
foreach (Transform t in spawnParent) {
    spawnPoints.Add(t);
}