Something I’ve been struggling to find out is how I would spawn a random tile, at a random position (from set positions) without them overlapping. Most of the tutorials I have seen have been for preventing overlap from random points in an area, rather than set positions.
I essentially have two arrays
One array is for the different tiles I want to be able to spawn
The other array is for all of the spawn positions (basically empty game objects at set coordinates) that I want to be able to spawn things at
My code is this:
public void SpawnTiles()
{
while(currentTileCount < tileLimit)
{
//Grab random tile prefab from array to spawn
int tSpawn = Random.Range(0, spawnables.Length);
//Grab random spawn position from an array of set spawn positions
int spawnLoc = Random.Range(0, spawnPos.Length);
GameObject.Instantiate(spawnables[tSpawn], spawnPos[spawnLoc].transform.position, Quaternion.identity);
currentTileCount++;
}
}
I’m unsure of how to prevent two tiles from spawning on top of one another.
One thought I had is that I could remove that spawn position from the array, disallowing it from being a possible spawn. But I would then need to add that position (and any other positions) I removed back to the array after I call a function that basically deletes all the tiles, and I’m unsure of how I’d do that. (assuming that’s the better way to do it, I’m not quite sure)
If anyone has any ideas, I’d love to hear them since I am fairly new to coding and don’t understand all too much