Hello! It's been a while since I've visited the forums, so I'm a little rusty.
If I understand correctly, you want to make an element to spawn N times without repeating position.
Here is a possible solution:
Keep a list of positions already used.
And when going for spawning a Prefab and selecting the new random position, make the random position generator to avoid those positions already used. A way to do this is by trial and error.
Some code to help you get the idea...
private List<Vector3> GenerateRandomPositions(Vector2 xRange, Vector2 yRange, Vector2 zRange, int amount) { List<Vector3> randomPositions = new List<Vector3>(); for (int i = 0; i < amount; i++) { Vector3 newRandomPosition = RandomPosition(xRange, yRange, zRange, randomPositions); randomPositions.Add(newRandomPosition); } return randomPositions; } private Vector3 RandomPosition(Vector2 xRange, Vector2 yRange, Vector2 zRange, List<Vector3> usedPositions) { Vector3 newRandomPosition; do { newRandomPosition = new Vector3( Random.Range(xRange.x, xRange.y), Random.Range(yRange.x, yRange.y), Random.Range(zRange.x, zRange.y)); } while (usedPositions.Contains(newRandomPosition)); return newRandomPosition; } private void ShowLogRandomPositions(List<Vector3> positions) { string report = string.Empty; foreach (var item in positions) { report += item.ToString() + "\n"; } Debug.Log("ShowLogRandomPositions: " + "\n" + report); } private void Start() { List<Vector3> randomPositions = GenerateRandomPositions(new Vector2(-2, 2), new Vector2(-2, 2), new Vector2(-2, 2), 15); ShowLogRandomPositions(randomPositions); }
The next thing from here is to get sure they don't step on each other. For that, you should take care of the size of the objects you're spawning so playing with the allowed positions around area/proximity should do the trick.
If this was what you're trying to reach for, I hope this helps.
If you have any questions, please ask.
Good luck!