ArgumentOutOfRangeException in runtime

Hello, I’m a beginner at unity and have been having difficulty on implementing a certain script

The main idea is that there are multiple spawners for a boy npc and they should be divided onto teams, this issue isconcerning the implementation of multiple spawn points for the boy prefab as I have already set up the arrays and child objects in the scene, any help is welcome

public class BoySpawner : MonoBehaviour
{
    [SerializeField]
    private BoyController boyPrefab = null;

    [SerializeField]
    private int amountToSpawn = 5;

    [SerializeField]
    private float timeBetweenSpawns = 2f;

    private SpriteSortingManager spriteSortingManager = null;

    private BoyManager boyManager = null;

    private BoyController boyController = null;

    [SerializeField]
    private Transform[] spawnPoints;

    [SerializeField]
    private GameObject[] boy;

    public List<BoyController> boys = new List<BoyController>();

    public void AddBoy(BoyController boyController)
    {
        if (!boys.Contains(boyController))
        {
            boys.Add(boyController);
        }
    }

    public void RemoveBoy(BoyController boyController)
    {
        boys.Remove(boyController);
    }

    public BoyController GetRandomBoy()
    {
        if (boys.Count > 0)
        {
            return boys[Random.Range(0, boys.Count)];
        }
        return null;
    }

    private void Awake()
    {
        spriteSortingManager = FindObjectOfType<SpriteSortingManager>();
        boyManager = FindObjectOfType<BoyManager>();
        boyController = FindObjectOfType<BoyController>();
    }

    private void Start()
    {
        StartCoroutine(SpawnBoys());
        foreach (var t in boyManager.boys)
        {
            print(t);
        }
    }

    private IEnumerator SpawnBoys()
    {
        WaitForSeconds timeToWait = new WaitForSeconds(timeBetweenSpawns);
        for (int i = 0; i < amountToSpawn; i++)
        {
            yield return timeToWait;
            SpawnBoy();
        }
    }

    private void SpawnBoy()
    {
        int numberEnemy = Random.Range(0, boys.Count);
        int randomPosition = Random.Range(0, spawnPoints.Length);

        //BoyController boyController = Instantiate(boyPrefab, transform.position, transform.rotation);

        //BoyController boyController = Instantiate(boyPrefab, spawnPoints[randomPosition].position, Quaternion.identity);

        Instantiate(boys[numberEnemy], spawnPoints[randomPosition].position, Quaternion.identity);

        int boyOrderInLayer = spriteSortingManager.GetSpriteOrderInLayer();
        boyController.SetOrderInLayer(boyOrderInLayer);


        boyManager.AddBoy(boyController);
    }

    //private BoyController Instantiate(BoyController boyPrefab, object position, Quaternion identity)
    //{
        //throw new System.NotImplementedException();
    //}
}

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

http://plbm.com/?p=236

Steps to success:

  • find which collection it is and what line of code accesses it <— critical first step!)
  • find out why it has fewer items than you expect
  • fix whatever logic is making the indexing value exceed the collection size
  • remember you might have more than one instance of this script in your scene/prefab
  • remember the collection may be used in more than one location in the code
  • remember that indices start at ZERO (0) and go to the count / length minus 1 (three (3) items are 0,1,2 only)

Surely the exception gave you a code along with the description and importantly the line number or at least the method? These are items we use to locate the cause of the error. I will assume that it is in GetRandomBoy. If there are (for example) 3 boys in the array then are boys[0], boys[1] and boys[2]. But your count will return 3 because that is how many there are. The last element is always 1 less than the total elements