Enemies spawn on top of each other

Hey I will try and explain my situation as best I can. Here is the issue:

I have a script that allows a maximum of 3 enemies to spawn in on start up. This is fine and they spawn at different empty gameobjects located around my map (15 in total, all stored in an array). Sometimes everything is fine and they all spawn at unique points and do not conflict with each other but sometimes, they choose the same spawn point (empty gameobject) as another enemy, thus leaving 2 enemies on top of each other. Here is some more information to help understand my setup:

I have 15 empty gameObjects located around my map. When the game starts, the 3 enemies instantiate at different locations ( random choice of the 15 on the map). Sometimes it works fine but other times they spawn on top of each other making it a possibility for you to hit 2 targets at once which is not what I want. Here is the code:

var car:Transform[];
var spawn:Transform[];

function Start(){
for(var k =0 ;k<3;k++){
var car:Transform = car[Random.Range(0,car.length)];
var spawnpos:Transform = spawn[Random.Range(0,spawn.length)];
Instantiate(car,spawnpos.position,spawnpos.rotation);

}
}

/ Up to now: The script spawns 3 enemies at random points but it is possible for them to use the same point. Is there anyway of stopping this from occurring, I’ve been hacking away at it for a while now but to no avail lol :frowning: Some users on here have given me solid advice and I have learnt a lot but none of it is relevant to what I actually need, so I hope someone can help. If you need any information please feel free to ask.

The fix: (Please read comment section first):

var car:Transform[];
var car2:Transform[];
var spawn:Transform[];
var spawn2:Transform[];

function Start(){
for(var k =0 ;k<1;k++){
var car:Transform = car[Random.Range(0,car.length)];
var spawnpos:Transform = spawn[Random.Range(0,spawn.length)];
Instantiate(car,spawnpos.position,spawnpos.rotation);

}

for(var t =0 ;t<1;t++){
var car2 : Transform = car2[Random.Range(0,car2.length)];
var spawnpos2:Transform = spawn2[Random.Range(0,spawn2.length)];
Instantiate(car,spawnpos2.position,spawnpos2.rotation);

}
}

Perhaps you could first do 3 “randoms”, check if theses randoms are all differents from each other. then use theses values when you select the spawn ?

Well I think you should save your (array index of the spawn position) → savedPositions = array();

When it makes a new random value, let it check if it already exists in your savedPositions array.

If not

→ make the monster span

else

→ make a new random value and check it.