Reputation: 87
I am developing a laser defender game.
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.
if (AllMembersDead())
{
for (float i = 0; i < waves; i++)
{
SpawnUntilFull(); // number of waves to be spawned
}
Destroy(position);
}
Here is the picture of the gameobject I destroy:
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.
Upvotes: 0
Views: 96
Reputation: 310
I think your AllMembersDead() function returns false because the childPositionGameObject you're looking at is the TotalPositions gameobject, hence SpawnUntilFull() is never called. Try changing the function to point to the right parent, as such:
bool AllMembersDead()
{
foreach(Transform childPositionGameObject in position)
{
if (childPositionGameObject.childCount > 0)
{
return false;
}
}
return true;
}
Upvotes: 1