Search Unity

SetActive doesn't activate a game object.

Discussion in 'Scripting' started by blablaalb, Aug 3, 2019.

  1. blablaalb

    blablaalb

    Joined:
    Oct 28, 2015
    Posts:
    53
    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:
    Code (CSharp):
    1. public IPoolMember SpawnEnemy(Vector3 position, EEnemies enemyType, bool setActive = true)
    2. {
    3.  
    4.     IPoolMember spawnedEnemy;
    5.     Transform enemyTransform;
    6.  
    7.     switch (enemyType)
    8.     {
    9.         case EEnemies.GrandmaEnemy:
    10.             //If pool isn't empty
    11.             if (_grandmaEnemyPool.Count > 0)
    12.             {
    13.                 //retrieve an enemy
    14.                 spawnedEnemy = _grandmaEnemyPool.Pop();
    15.             }
    16.             else
    17.             {
    18.                 //else if pool is empty
    19.                 //instantiate a new grandma enemy.
    20.                 spawnedEnemy = InstantiateNewEnemy(enemyType);
    21.             }
    22.  
    23.             //get transform component;
    24.             enemyTransform = spawnedEnemy.GetTransform;
    25.  
    26.             //set position for enemy.
    27.             enemyTransform.position = position;
    28.             break;
    29.          
    30.  
    31.         case EEnemies.RunnerEnemy:
    32.             //If pool isn't empty
    33.             if (_runnerEnemyPool.Count > 0)
    34.             {
    35.                 //retrieve an enemy
    36.                 spawnedEnemy = _runnerEnemyPool.Pop();
    37.             }
    38.             else
    39.             {
    40.                 //else if pool is empty
    41.                 //instantiate a new runner enemy.
    42.                 spawnedEnemy = InstantiateNewEnemy(enemyType);
    43.             }
    44.  
    45.             //get transform component;
    46.             enemyTransform = spawnedEnemy.GetTransform;
    47.  
    48.             //set position for the enemy.
    49.             enemyTransform.position = position;
    50.             break;
    51.  
    52.         case EEnemies.AlphaEnemy:
    53.             //If pool isn't empty
    54.             if (_alphaEnemyPool.Count > 0)
    55.             {
    56.                 //retrieve an enemy
    57.                 spawnedEnemy = _alphaEnemyPool.Pop();
    58.             }
    59.             else
    60.             {
    61.                 //else pool is empty
    62.                 //instantiate a new alpha enemy
    63.                 spawnedEnemy = InstantiateNewEnemy(enemyType);
    64.             }
    65.  
    66.             //get transform component;
    67.             enemyTransform = spawnedEnemy.GetTransform;
    68.  
    69.             //set position.
    70.             enemyTransform.position = position;
    71.             break;
    72.  
    73.         default:
    74.             throw new UnityException(string.Format("Unknown enemy type: {0}.", enemyType.ToString()));
    75.     }
    76.  
    77.     spawnedEnemy.GetTransform.gameObject.SetActive(setActive);
    78.     return spawnedEnemy;
    79. }
    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?
     
  2. ismaelflorit

    ismaelflorit

    Joined:
    Apr 16, 2019
    Posts:
    38
    Hey,

    What does your IPoolMember interface look like?
     
  3. blablaalb

    blablaalb

    Joined:
    Oct 28, 2015
    Posts:
    53
    Hi @ismaelflorit. Thank you for your attention but I already solved the problem. Turned out the problem was in the FSM logic. After enemy was killed. returned back to the pool and then retrieved from the pool, it still remained in the Death state. The Death state was deactivating the enemy after it was retrieved from the pool.