Other
I need help with my Transform Method,I need help with my code! Unasked
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; }
Answer by meat5000 · Apr 03, 2018 at 12:19 PM
I presume if (childPositionGameObject.childCount == 0)
Is your state of no children which should return null? But when its 0 you have it to return the transform instead of null, so NextFreePosition is not null and will be true?
~
Im not sure this looks right
foreach (Transform childPositionGameObject in transform)
Perhaps you wanted
foreach (Transform childPositionGameObject in EnemyFormation)
?