Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can anyone explain this bug with my List?

Discussion in 'Scripting' started by Hero101, May 8, 2016.

  1. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    I have a GameManager class attached to an empty game object and in it I create a new public list and fill it with game objects in the inspector. When I do Debug.Log (lifeArray.Count) it will be 3 which is what it should be as I filled it with 3 objects.

    Now I can do:
    Code (CSharp):
    1. Debug.Log (lifeArray.Count)
    Anywhere in my GameManager class and it will always read 3....BUT....I have one function called in my GameManager class called respawnPlayer() that gets called from inside an OnTriggerEnter function attatched to all of the enemies in the game.

    Whenever I do:
    Code (CSharp):
    1. Debug.Log (lifeArray.Count)
    inside of the respawnPlayer function it reads 0. I don't understand why. Nowhere in my code to I manipulate the List.
     
  2. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    we'd be better able to assist you if you provide more code.
     
  3. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    Here is the code for my GameManager class:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class GameManager : MonoBehaviour {
    8.  
    9.     public GameObject player;
    10.     //public GameObject gun;
    11.     //public GameObject playerBullet;
    12.     private Vector3 playerSpawn = new Vector3 (3, 0.1f, 0);
    13.     public Vector3 landSpawn;
    14.     public List<GameObject> lands = new List<GameObject>();
    15.     private int index;
    16.     //public Quaternion gunRotation;
    17.     public static bool gameOver;
    18.     public static int lives;
    19.  
    20.     //UI FOR LIVES
    21.     public List<GameObject> lifeList; //HERE IS THE ARRAY I CREATED
    22.  
    23.     public float time;
    24.     private float seconds;
    25.     private float startTime = 0f;
    26.  
    27.     public static float score;
    28.  
    29.  
    30.     // Use this for initialization
    31.     void Start ()
    32.     {
    33.         gameOver = true;
    34.         NewGame();
    35.     }
    36.    
    37.     // Update is called once per frame
    38.     void Update ()
    39.     {
    40.         if (!gameOver)
    41.         {
    42.             gameState ();
    43.             time = (int)(seconds += Time.deltaTime);
    44.             enemyProgression ();
    45.         }
    46.         else
    47.         {
    48.             CancelInvoke();
    49.  
    50.         }
    51.     }
    52.  
    53.     void FixedUpdate ()
    54.     {
    55.        
    56.     }
    57.  
    58.     void NewGame ()
    59.     {
    60.         time = startTime;
    61.         score = 0f;
    62.         Instantiate(lands[0], landSpawn, Quaternion.identity);
    63.         InvokeRepeating("spawnLand", 0f, 6.3f);
    64.         lives = 3;
    65.         Debug.Log(lifeList.Count); //SHOWS 3 CUZ I FILLED IT WITH GAMEOBJECTS IN INSPECTOR
    66.         Instantiate(player, playerSpawn, Quaternion.identity);
    67.  
    68.  
    69.         gameOver = false;
    70.     }
    71.  
    72.     void gameState ()
    73.     {
    74.         if (lives <= 0)
    75.         {
    76.             gameOver = true;
    77.             endGame();
    78.         }
    79.     }
    80.  
    81.     public void respawnPlayer ()
    82.     {
    83.         if (!gameOver)
    84.         {
    85.            Debug.Log(lifeList.Count); //SHOWS ZERO ANYWHERE IN respawnPlayer function.
    86.             if (lives > 0)
    87.             {
    88.                 Instantiate (player, playerSpawn, Quaternion.identity);
    89.             }
    90.         }
    91.     }
    92.  
    93.     private void spawnLand ()
    94.     {
    95.         index = Random.Range (0, lands.Count);
    96.         Instantiate(lands[index], landSpawn + (lands[index].transform.right * 32), Quaternion.identity);
    97.     }
    98.  
    99.     private void endGame ()
    100.     {
    101.         Scene scene = SceneManager.GetActiveScene();
    102.         SceneManager.LoadScene(scene.name);
    103.     }
    104.  
    105.  
    106.     private void enemyProgression ()
    107.     {
    108.         if (time >= 30)
    109.         {
    110.            
    111.         }
    112.     }
    113. }
    114.  
    I commented areas in code pertaining to the List. The respawnPlayer() function in the above class gets called from a script attached to an enemy:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class AsteroidController : MonoBehaviour {
    8.  
    9.     public GameManager gameManager;
    10.  
    11.     private float xSpeed = -3f;
    12.     private float ySpeed = -3f;
    13.  
    14.     // Use this for initialization
    15.     void Start ()
    16.     {
    17.         if (transform.position.x < 10)
    18.         {
    19.             xSpeed *= -1;
    20.         }
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update ()
    25.     {
    26.         transform.Translate (xSpeed * Time.deltaTime, ySpeed * Time.deltaTime, transform.position.z, Camera.main.transform);
    27.     }
    28.  
    29.     void OnTriggerEnter(Collider other)
    30.     {
    31.         if (other.tag == "Player")
    32.         {
    33.             Destroy (other.gameObject);
    34.             Destroy (gameObject);
    35.             GameManager.lives--;
    36.             gameManager.respawnPlayer();
    37.         }
    38.     }
    39. }
    40.  
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Is it possible you have mistakenly created two instances of your GameManager component? One with the inspector filled out, one without.
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Calling Destroy(gameObject); on line 34 before gameManger.respawnPlayer() might be the issue