# Switch spawn position of tiles

Hello!

I am making a sidescroller game with random terrain. I have made a function which spawns tiles at four random y-coordinates. What i want to implement so that a new set of tiles cant spawn at the y-coordinate that the previous set of tiles has spawned on.

This is my code for the function:

``````void Spawntiles(){

GameObject[] temp = new GameObject[randLength];
newTiles = temp;

randLength = Random.Range(minLength,maxLength);
index = Random.Range (0, y.Length);
holeChance = Random.Range (1, 7);

for (int i = 0; i < newTiles.Length; i++){
newTiles *= Instantiate(plainTile,new Vector3(lastTile.transform.position.x+x+2.2f,y[index].position.y,0),transform.rotation) as GameObject;*
``````
• `````` 	x = x + 2.4f;*
``````
• `````` }*
``````
• `````` spawnTiles = false;*
``````
• `````` lastTile = newTiles [newTiles.Length-1];*
``````
• }*
The y coords are given by four public empty gameobjects that are positioned at the desired spawnpoints. These are in an array. The line index = Random.Range (0, y.Length); picks a number which is then used in the for loop to choose the gameobject of which the tile spawns on: y[index].position.y.
Anyway, if someone has done anything like this or can come up with something id appretiate the input.
Thank you!

You could create a list of the currently used positions and when generating a new position check to see if itâ€™s already been used if so just generate and check again!