Spawn an object to a random spawn point from a list

I want to spawn 3 of the same game object at a random spawn point that I put into a list. But everytime I run my project there’s an error saying ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

I was wandering how does this error is happening, or there can be a better way to do what i am trying to implement?

private void Start()
{
    foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Savee"))
    {
        saveePoints.Add(obj);
    }
    spawn();
}

void spawn()
{
    for (int i = 0; i <= 2; i++)
    {
        i = Random.Range(0, saveePoints.Count);
        saveePoints _= Instantiate(saveeObject, saveePoints*.transform.position, transform.rotation) as GameObject;*_

}
}

You are changing the value of i inside your for loop, which you are already using to cycle through it three times. Change the i to j or similar, perhaps that helps?

You shouldn’t use i to count your instances and to get a random spawn position. Try something like this.

 void spawn()
 {
     for (int i = 0; i <= 2; i++)
     {
         int spawnIndex = Random.Range(0, saveePoints.Count);
         Instantiate(saveeObject, saveePoints[spawnIndex].transform.position, transform.rotation);
     }
 }

If the error is still showing after, make sure your Start function actually finds the spawn positions.

Also, I don’t understand why you replace your spawn positions with your new instances… Can you tell what you are trying to do exactly?