Search Unity

List keeps appearing as empty.

Discussion in 'Scripting' started by gameangel147, Jan 9, 2019.

  1. gameangel147

    gameangel147

    Joined:
    Oct 16, 2016
    Posts:
    26
    So I'm pooling some turrets and trying to make a system to place turrets on the map. In the GameController script I fill the turret1Pool list with five turrets, and I can see it's full in the inspector, since I serialized it to check. In another script, when the player clicks on the turret icon, OnMousUpAsButton will call the PlaceTurret() function in my GameController script and grab the first element in that script.

    The problem is that I get an "IndexOutOfRange" exception, and when I print the Count of the list, it says 0. Yet when I print the list count anywhere else in GameController, it says 5. I don't understand why the list is empty just in this function, but if I try to manipulate the list anywhere else in GameController it works.

    I am using an enum and a Switch statement to control the state of the game, so I'm wondering if that might have something to do with it.

    Note: The PlaceTurret function is the function in question here. Also I deleted most of the code in the functions that isn't relevant to make it easier to read.

    Code (CSharp):
    1.  
    2. public class GameController : MonoBehaviour
    3. {
    4.     private enum GamePhase { GameSetup, PlayerSetup, GamePhase };
    5.     private GamePhase currentGamePhase;
    6.  
    7.     [SerializeField] private GameObject turret1Ref;
    8.  
    9.     [SerializeField] private Transform turret1SpawnLoc;
    10.  
    11.     [SerializeField] public List<GameObject> turret1Pool = new List<GameObject>();
    12.     private List<GameObject> currentTurretList = new List<GameObject>();
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         switch (currentGamePhase)
    18.         {
    19.             case GamePhase.GameSetup:
    20.                 RunGameSetup();
    21.                 break;
    22.  
    23.             case GamePhase.PlayerSetup:
    24.                 RunPlayerSetup();
    25.                 break;
    26.  
    27.             case GamePhase.GamePhase:
    28.                 StartGame();
    29.                 break;
    30.         }
    31.     }
    32.  
    33.     /////////////////////////////////////////////////////////////////////////////////////////
    34.  
    35.     private void RunGameSetup()
    36.     {
    37.         SpawnEnemies();
    38.         SpawnTurrets();
    39.         SetupLevel();
    40.         currentGamePhase = GamePhase.PlayerSetup;
    41.     }
    42.  
    43.     private void SpawnEnemies()
    44.     {
    45.     }
    46.  
    47.     private void SpawnTurrets()
    48.     {
    49.         for (int j = 0; j < 5; j++)
    50.         {
    51.             GameObject newTurret1 = Instantiate(turret1Ref, turret1SpawnLoc.position, Quaternion.identity);
    52.             newTurret1.GetComponent<TurretModule>().currentTurretState = TurretModule.TurretState.Pooled;
    53.             turret1Pool.Add(newTurret1);
    54.         }
    55.     }
    56.  
    57.     private void SetupLevel()
    58.     {
    59.     }
    60.  
    61.     /************************************************************************************/
    62.  
    63.     public void PlaceTurret()
    64.     {
    65.         print(turret1Pool.Count);
    66.         turret1Pool[0].GetComponent<TurretModule>().currentTurretState = TurretModule.TurretState.Placement;
    67.     }
    68.  
    69.     private void RunPlayerSetup()
    70.     {
    71.         PlayerTowerSetup();
    72.     }
    73.  
    74.     private void PlayerTowerSetup()
    75.     {
    76.        
    77.     }
    78.  
    79.     private void StartGame()
    80.     {
    81.     }
    82. }
    83.  
    84.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Make sure you are not replacing the turret1Pool list with a new list somewhere else. Make sure you don't have more than 1 instance of this script in your scene. I suspect you're accessing the wrong instance of the script when calling PlaceTurret().