# How cound instantiate all objects in a list ?

I have a list that has 4 gameobject.I want to instantiate gameobject one to for example position.x 1,and object 2 ,position.x 2, How can I do this?

Create an array or list of positions the same length as list of gameobjects. Loop through array of gameobjects and assign positions.

``````public List<GameObject> gameobjects = new List <GameObject>();
public Vector3[] positions = new Vector3[gameobjects.Count];

void Start () {
for (int i = 0; i <gameobjects.Count; i++) {
Instantiate (gameobjects_, positions*, Quaternion.identity);*_
``````

}
}

Based on whatever I understand from your response to my comment under question, you need to instantiate one gameobject from your list each time certain button is pushed. Also these gameobjects will be instantiated in a row where each gameobject will get appended to next column of last instantiated gameobject. The code for this can be written like:

``````public List myList<GameObject> = new List<GameObject>();

// Position for first gameobject to spawn at.
public Vector2 firstPos;
public float distance = 10f;

private int lastInstanceIndex = -1;

// Call this method from your button click. It will instantiate one gameoject each time the button is clicked.
public void InstantiateGo()
{
int indexToSpawn = lastInstanceIndex + 1;
// We have reached the end of the list. No more gameobjects to spawn.
if(indexToSpawn >= myList.Count) {
return;
}

float posX = firstPos + (indexToSpawn * distance);
Vector2 pos = new Vector2(posX, firstPos.y);
GameObject instance = Instantiate (gameobjects[indexToSpawn], pos, Quaternion.identity) as GameObject;
if(instance != null) {
lastInstanceIndex = indexToSpawn;
}
}
``````