I am developing a tank game which is similar to Tank 1990. I mean that it is not the close game, just similar.
link: https://play.google.com/store/apps/details?id=com.n4tech.t903d
. Each scene, game instantiate several tanks in order. My question is: How to instantiate Tanks in order ?
Ex:
scene 1: Tank1, Tank1, Tank1, Tank1, Tank2, Tank2, Tank2, Tank2
scene 2: Tank2, Tank2, Tank2, Tank2, Tank1, Tank1, Tank1, Tank1,
scene 3: Tank2, Tank1, Tank2, Tank1, Tank2, Tank1, Tank1, Tank1,
Just declare an array of GameObjects, and fill it with the right prefabs in the right order. Just loop through the array to instantiate your tanks.
public GameObject[] tankPrefabArray = new GameObject[1];//assign via inspector (or via code if you wish)
void Start() {
foreach(GameObject tankPrefab in tankPrefabArray) {
GameObject newTank = Instantiate(tankPrefab, position, rotation) as GameObject;
}
}
If you don’t want to instantiate the tanks at the same time, but rather by calling a method when certain conditions apply (such as the last tanks having been destroyed), use an index that increases.
public GameObject[] tankPrefabArray = new GameObject[1];//assign via inspector (or via code if you wish)
public int tankSpawnIndex = 0;
public void SpawnTank() {
if(tankSpawnIndex < tankPrefabArray.Length) {
GameObject newTank = Instantiate(tankPrefabArray [tankSpawnIndex ], position, rotation) as GameObject;
tankSpawnIndex ++;
}
else {
Debug.Log("End of array reached.");
}
}