I can't understand why NextFreePosition() becomes true when all the enemies are destroyed.
EX: we have an EnemyFormation Transform as a father.
He's got 3 children named Position.
When you play the game, each position develops a child called enemy that represents a ship.
When you destroy an enemy, the position no longer has an enemy as a child.
1) What i don't understand is why NextFreePosition() returns true when all the enemies are destroyed.
2) childPositionGameObject.childCount represents the children of Position?
3) Shouldn't NextFreePosition() return childPositionGameObject every time an enemy is destroyed?
4) What does it mean for NextFreePosition() to become true?
void SpawnUntilFull() { Transform freePosition = NextFreePosition(); if(freePosition) { GameObject enemy = Instantiate(enemyPrefab, freePosition.position, Quaternion.identity) as GameObject; enemy.transform.parent = freePosition; } if (NextFreePosition() == true) //** Invoke("SpawnUntilFull", spawnDelay); } Transform NextFreePosition() { foreach (Transform childPositionGameObject in transform) { if (childPositionGameObject.childCount == 0) { Debug.LogError("Position returned!"); return childPositionGameObject; } } return null; }