How to randomly put my game objects in 3 previously setup vectors2 without repeating 2 objects in the same Vector2?

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:

  1. store your vector2 position in a List
  2. randomly draw your first vector2 position from the list
  3. remove the vector2 position your draw from the list
  4. goto 2 :slight_smile:

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);