Objects Spawn on top of each other?

Here is my code can someone please help me to have these objects not spawn on top of each other?

using UnityEngine;
using System.Collections;
 
public class codecoin : MonoBehaviour
{
 
  public Transform[] spawnPoints;
  public GameObject[] enemyPrefabs;
  public int amountEnemies = 20;  // Total number of enemies to spawn.
  public int yieldTimeMin = 0;
  public int  yieldTimeMax = 5;  // Don't exceed this amount of time between spawning enemies randomly.
  public int i;
 
	void Update() {
	yieldTimeMin =  PlayMakerGlobals.Instance.Variables.GetFsmInt("coinmin").Value;
	yieldTimeMax = 	PlayMakerGlobals.Instance.Variables.GetFsmInt("coinmax").Value;
	amountEnemies = PlayMakerGlobals.Instance.Variables.GetFsmInt("coinamount").Value;
	}
	
  public IEnumerator Spawnblock() 
  { 
      for (i=0; i<amountEnemies; i++)
      {
        yield return new WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.
 
        var obj = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)]; // Randomize the different enemies to instantiate.
        var pos = spawnPoints[Random.Range(0, spawnPoints.Length)];  // Randomize the spawnPoints to instantiate enemy at next.
 
        Instantiate(obj, pos.position, pos.rotation); 
      }
  }
 
}

Is this C# or javascript? I thought only javascript used “var” while C# has your declarations at the top (i.e. public int amountEnemies = 20). You might have mixed both types of coding into one script.

If that’s not the case, either your spawnPoints array is only one element, or the transforms in that array are all set to the same position. Did you check both of those? Might be trivial, but it slips by sometimes.

Also, you can assign a variable randNum and have

 for (i=0; i<amountEnemies; i++)
      {
        var randNum: int = Random.Range(0, spawnPoints.Length); //ADDED THIS LINE
        yield return new WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.
 
        var obj = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)]; // Randomize the different enemies to instantiate.
        var pos = spawnPoints[randNum];  // Randomize the spawnPoints to instantiate enemy at next. CHANGED THIS LINE.
        print (randNum);
 
        Instantiate(obj, pos.position, pos.rotation); 
     }

This will check to make sure that your number keeps changing.

I remember having a problem with this as well and I just broke it down with debug statements as much as possible to find the error in my ways. It eventually got fixed, so yours can too!