I have RoundManager script which’s subscribed to the EnemyKilled event. Every time an enemy is killed it spawns an enemy on random spawn point. To spawn an enemy the RoundManager claims the enemy from the EnemyPool object. This’s the EnemyPool’s code snippet which returns the enemy:
public IPoolMember SpawnEnemy(Vector3 position, EEnemies enemyType, bool setActive = true)
{
IPoolMember spawnedEnemy;
Transform enemyTransform;
switch (enemyType)
{
case EEnemies.GrandmaEnemy:
//If pool isn't empty
if (_grandmaEnemyPool.Count > 0)
{
//retrieve an enemy
spawnedEnemy = _grandmaEnemyPool.Pop();
}
else
{
//else if pool is empty
//instantiate a new grandma enemy.
spawnedEnemy = InstantiateNewEnemy(enemyType);
}
//get transform component;
enemyTransform = spawnedEnemy.GetTransform;
//set position for enemy.
enemyTransform.position = position;
break;
case EEnemies.RunnerEnemy:
//If pool isn't empty
if (_runnerEnemyPool.Count > 0)
{
//retrieve an enemy
spawnedEnemy = _runnerEnemyPool.Pop();
}
else
{
//else if pool is empty
//instantiate a new runner enemy.
spawnedEnemy = InstantiateNewEnemy(enemyType);
}
//get transform component;
enemyTransform = spawnedEnemy.GetTransform;
//set position for the enemy.
enemyTransform.position = position;
break;
case EEnemies.AlphaEnemy:
//If pool isn't empty
if (_alphaEnemyPool.Count > 0)
{
//retrieve an enemy
spawnedEnemy = _alphaEnemyPool.Pop();
}
else
{
//else pool is empty
//instantiate a new alpha enemy
spawnedEnemy = InstantiateNewEnemy(enemyType);
}
//get transform component;
enemyTransform = spawnedEnemy.GetTransform;
//set position.
enemyTransform.position = position;
break;
default:
throw new UnityException(string.Format("Unknown enemy type: {0}.", enemyType.ToString()));
}
spawnedEnemy.GetTransform.gameObject.SetActive(setActive);
return spawnedEnemy;
}
You can see that on the line 77 before returning the enemy, the EnemyPool activates it.
The problem is that sometimes the returned enemy is active sometimes it’s not. I can’t figure out why. I tried to debug the EnemyPool’s SpawnEnemy method. I placed a breakpoint on the line 78, right on the return statement with the following condition: spawnedEnemy.GetTransform.gameObject.activeSelf == false
and this is what got:
I can’t figure out why it’s deactivated?