Hi! Firstly, I only want to give you enough of an understanding so you can give writing what you want a go. Iβm going to break down your problem into a number of steps:
- Choosing a random prefab to instantiate
- Create a spawn method
- Call the spawn method repeatedly
Choosing a random prefab to instantiate
To do this we need a public array of GameObjects (prefabs) to choose from. This is done by adding ββ to the end of the variable type (itβs usually best to make the array name a plural):
public GameObject[] enemyPrefabs;
Now to access an element or single prefab you pass in an index between 0 and the number of entries in the array or βLengthβ property. So to get a random entry you would use:
GameObject enemyPrefab = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
Then you can use that variable to instantiate an enemy in the scene.
Create a spawn method
Because you are now spawning enemies at the start of the game, and then periodically throughout the game, you should wrap this functionality in one method that you can call repeatedly whenever you require an enemy to be spawned. For example:
private void SpawnEnemy()
{
Vector3 newPos = new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), 0);
GameObject octo = Instantiate(enemyPrefab, newPos, Quaternion.identity) as GameObject;
}
// Example of calling method in Start...
for (int i = 0; i < numEnemies; i++)
{
SpawnEnemy();
}
Call the spawn method repeatedly
For repeated spawn there are many ways to do this. What you suggest is simple, you want a countdown to each spawn. For this you simply need to add an Update method and a time variable to track how long until the next spawn. Subtract the delta time from your countdown, and when the countdown reaches <= 0 spawn your enemies and reset the timer:
public float spawnCooldown = 1;
private float timeUntilSpawn = 0;
public void Update()
{
timeUntilSpawn -= Time.deltaTime;
if(timeUntilSpawn <= 0)
{
// Do your enemy spawns here
...
// Reset for next spawn
timeUntilSpawn = spawnCooldown;
}
}
I hope that helps explain how to approach what you wanted. =D
Please post your script.
β tanoshimiyou can't choose a member of platformPrefabs until the code knows it has been initiated, try: public GameObject[] platformPrefabs; GameObject platformPrefab; private void SpawnPlatform(){ if(platformPrefabs == null || platformPrefabs.Length < 1){ return; } platformPrefab = platformPrefabs[Random.Range(0, platformPrefabs.Length)]; Vector3 newPos = new Vector3 (3.5f, 1.8f, 0f); GameObject octo = Instantiate (platformPrefab, newPos, Quaternion.identity) as GameObject; }
β Scribe