- Home /
Question by
GhettoBurger996 · Nov 28, 2017 at 05:56 PM ·
c#programming
Method to stop enemies from spawning Unity5
I am developing a laser defender game as part of the course "Learn how to program in C# by developing games".
Here is the main function responsible for spawning enemies. I have the variable waves set to 3. Then I want to destroy the position of the space ships in order to not allow them to spawn. The issue is when I start the game there are no ships at all. (The method below is in the update Function)
if (AllMembersDead())
{
for (float i = 0; i < waves; i++)
{
SpawnUntilFull(); // number of waves to be spawned
}
Destroy(position);
}
Here is a picture of the gameObject. https://i.stack.imgur.com/a9pak.png
Here are the function for SpawnUntilFull() and AllMembersDead() if needed.
bool AllMembersDead()
{
foreach(Transform childPositionGameObject in transform)
{
if (childPositionGameObject.childCount > 0)
{
return false;
}
}
return true;
}
void SpawnUntilFull()
{
Transform freePosition = NextFreePosition();
if (freePosition)
{
GameObject enemy = Instantiate(enemyPrefab, freePosition.transform.position, Quaternion.identity) as GameObject; // Instantiate (spawning or creating) enemy prefab (as gameobject assures that what its returning to us is no normal object but rather a game object)
enemy.transform.parent = freePosition; // the transform of whatever thing the enemy is attached to, and that would be the enemyFormation GameObject
}
if (NextFreePosition())
{
Invoke("SpawnUntilFull", spawnDelay);
}
I'm not sure what I'm doing wrong. Any guidance would be appreciated.
Comment