Ok, cool, thanks for the clarification. If it is only happening occasionally, this is a standard behavior of a random number generator, you will occasionally get the same result. If you want to eliminate dupes, what you can do is register each position after you create it, then surround the random generation line with a do while loop that checks the result against your registered list. If it already exists continue, otherwise break out.
something like this:
List<Vector2> positions = new List<Vector2>();
while (planetcount < MaxPlanets)
{
Vector2 pos;
do
{
pos = new Vector2(Random.Range(-260, 260), Random.Range(-260, 260));
} while (positions.Contains(pos));
positions.Add(pos);
Instantiate(prefab, new Vector3(pos.x, 0, pos.y), Quaternion.Euler(0, 0, 0));
planetcount++;
}
Alternatively you could just compare one value (x or y) to prevent the same orbit and not just identical placement
To check just the Orbit (lets assume your Y is the distance)
List<int> positions = new List<int>();
while (planetcount < MaxPlanets)
{
Vector2 pos;
do
{
pos = new Vector2(Random.Range(-260, 260), Random.Range(-260, 260));
} while (positions.Contains(pos.y));
positions.Add(pos.y);
Instantiate(prefab, new Vector3(pos.x, 0, pos.y), Quaternion.Euler(0, 0, 0));
planetcount++;
}
To check for dupes within a given allowance, try the following:
List<int> positions = new List<int>();
int minDistance = 3;
while (planetcount < MaxPlanets)
{
Vector2 pos;
bool is_dupe= true;
while(is_dupe)
{
pos = new Vector2(Random.Range(-260, 260), Random.Range(-260, 260));
is_dupe = false
foreach(int orbit in positions)
{
if(Mathf.Abs(orbit - pos.y) < minDistance)
{
is_dupe = true;
}
}
}
positions.Add(pos.y);
Instantiate(prefab, new Vector3(pos.x, 0, pos.y), Quaternion.Euler(0, 0, 0));
planetcount++;
}