I’m trying to make a level that the objects spawn randomly between 3 vectors2, each one containing a different object. The problem is that i can’t think of a way to do this without 2 objects getting on the same position. Is there any way to use random.range that it would not repeat?
No there’s no parameter in Random.Range() to make it not repeat. So you need to manage your random logic.
Here’s how you can do it:
- store your vector2 position in a List
- randomly draw your first vector2 position from the list
- remove the vector2 position your draw from the list
- goto 2
in code, it would be something like that :
// create your list
List<Vector2> positionList = new List<Vector2>();
// add your vector 2 positions in the list
positionList.Add(Vector2.left);
positionList.Add(Vector2.right);
positionList.Add(Vector2.down);
// randomly draw your first position
int positionIndex = Random.Range(0, positionList.Count);
Vector2 rndPosition = positionList[positionIndex];
//... assign the rndPosition...
// then remove this vector2 position form the list, so your list contains only valid positions
positionList.RemoveAt(positionIndex);