Spawning limited GameObjects at a specific position not working


I’m trying to spawn in the scene 7 GameObjects from an array of prefabs. Currently the script spawns 7 object from the array (the 7 GameObjects are the same one all the time) and the every GameObject is spawn in the same position as the other ones. What I’m trying to achive is to pick up 7 GameObjects from the array (it can not be any one duplicated) and spawn in scene at the same position that GameObject prefab has. This is the script I’m using:

public GameObject [] luggageDestiny;
public Vector3 destinyValues;

void Start () {
	GameObject destinySpawn = luggageDestiny [Random.Range (0, luggageDestiny.Length)];
	for (int j = 0; j < luggageDestiny.Length; j++) {
		GameObject clone = (GameObject)Instantiate(destinySpawn, destinySpawn.transform.position, destinySpawn.transform.rotation);
		luggageDestiny [j] = clone;

The solution looks something like this:

using System.Collections.Generic;

public GameObject[] luggageDestiny;
GameObject[] spawnedObjects;

void Start () 
	spawnedObjects = new GameObject[7];

	List<int> usedIndices = new List<int>();

	for(int j = 0; j < spawnedObjects.Length; j++)
		int randomIndex = Random.Range(0, luggageDestiny.Length);
		while(usedIndices.Contains(randomIndex)) randomIndex = Random.Range(0, luggageDestiny.Length);
		GameObject clone = (GameObject) GameObject.Instantiate(luggageDestiny[randomIndex], luggageDestiny[randomIndex].transform.position, luggageDestiny[randomIndex].transform.rotation);
		spawnedObjects[j] = clone;

The code first creates an array of gameobjects, named ‘spawnedObjects’. This will contain the objects you are spawning, so you can use them later if you want.

The for loop executes 7 times. Each time it generates a random index from 0 to the length of the prefabs (50 in this case). If we have already spawned a prefab with that index, it generates a new one. (Note: this method is not very efficient if the luggageDestiny array has 10 elements, but in this case, it will be fine.)

After this, the code will instantiate the gameobject with the prefab’s original position and rotation. If you want to place them according to their index in the luggageDestiny array, you should use this line instead:

GameObject clone = (GameObject) GameObject.Instantiate(luggageDestiny[randomIndex], new Vector3(randomIndex, randomIndex, randomIndex), luggageDestiny[randomIndex].transform.rotation);

Or if you want to place them according to their line number (so the 1th spawned prefab will spawn to new Vector3(1, 1, 1), the 2nd will spawn to new Vector3(2, 2, 2), etc.), you should use this line:

GameObject clone = (GameObject) GameObject.Instantiate(luggageDestiny[randomIndex], new Vector3(j, j, j), luggageDestiny[randomIndex].transform.rotation);

The rotation can be modified as well. Right now, every prefab will spawn with the original rotation it has. You can use the same methods to apply rotation based on their index or line number, but you have to use a new method:

GameObject clone = (GameObject) GameObject.Instantiate(luggageDestiny[randomIndex], new Vector3(j, j, j), Quaternion.Euler(j, j, j));

(EDIT: i’ve forgot to mention that you have to insert ‘using System.Collections.Generic’ before the name of the class)