Hi! So I am kind of new to Unity and C#, and I wonder how you can Instantiate GameObjects if there are less than 5 in the Scene.
Could someone maybe please help me. Thank you!
Here’s the code
void Start()
{
StartSpawning();
}
void SpawnEnemy()
{
Instantiate (enemyPrefab[Random.Range(0,enemyPrefab.Length)], transform.position, Quaternion.identity);
}
void StartSpawning()
{
InvokeRepeating("SpawnEnemy", spawnTimer, spawnTimer);
}
void StopSpawning()
{
CancelInvoke();
}
}
,So I am kind of new to Unity and C#, and I wonder how you can Instantiate GameObjects , but only if there is less than 5 already in the Scene
Here is the code:
[SerializeField] GameObject enemyPrefab;
[SerializeField] float spawnTimer = 5f;
void Start()
{
StartSpawning();
}
void SpawnEnemy()
{
Instantiate (enemyPrefab[Random.Range(0,enemyPrefab.Length)], transform.position, Quaternion.identity);
}
void StartSpawning()
{
InvokeRepeating("SpawnEnemy", spawnTimer, spawnTimer);
}
void StopSpawning()
{
CancelInvoke();
}