how to use multiple spawn points

I’m trying to spawn enemies from 4 different spawn points I have the 4 spawn points set as empty game objects which are children of the spawn manager game object. I have found some code to put them in an array but I cant seem to get the vector3 position from the array to use when I instantiate an enemy. This is the code I am trying to use any help would be much apricated as I’m still learning everything.

public class SpawnManager : MonoBehaviour
    private GameObject enemyPrefab;
    private int spawnIndex;
    private Transform[] spawnpoints;
    private Vector3 spawnPos;
    // Start is called before the first frame update
    void Start()
        spawnpoints = GetComponentsInChildren(typeof(Transform)) as Transform[];
        spawnIndex = Random.Range(0, spawnpoints.Length);
        InvokeRepeating("spawnEnemys", 1, 5);

    void spawnEnemys()
        Instantiate(enemyPrefab, spawnpoints[spawnIndex], enemyPrefab.transform.rotation);

GetComponentsInChildren will give you not only transforms of the spawn points but also transform of the SpawnManager. So it is better to fill “spawnpoints” array manually:

	count = transform.childCount;
	spawnpoints = new Transform[count];
	for(int i = 0; i < count; i++){
		spawnpoints *= transform.GetChild(i);*
  • }*
    Next, if you assign spawnIndex in the Start method you will do it only once. All the time enemies will be instantiated in the same spawn point, which is not what you want. So assignment of the spawnIndex should be done in the “spawnEnemys” method. It will guarantee that enemies will appear each time in different place. Here’s the script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

public class SpawnManager : MonoBehaviour

  • [SerializeField]*

  • private GameObject enemyPrefab;*

  • private int spawnIndex;*

  • private Transform spawnpoints;*

  • private Vector3 spawnPos;*

  • private int count;*

  • void Start(){*

  •  count = transform.childCount;*
  •  spawnpoints = new Transform[count];*
  •  for(int i = 0; i < count; i++){*

_ spawnpoints = transform.GetChild(i);_
* }*

* InvokeRepeating(“spawnEnemys”, 1, 5);*
* }*

* void spawnEnemys(){*
* spawnIndex = Random.Range(0, count);*

* Instantiate(enemyPrefab, spawnpoints[spawnIndex].position, enemyPrefab.transform.rotation);*
* }*