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:
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:
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.
we’d be better able to assist you if you provide more code.
Here is the code for my GameManager class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public GameObject player;
//public GameObject gun;
//public GameObject playerBullet;
private Vector3 playerSpawn = new Vector3 (3, 0.1f, 0);
public Vector3 landSpawn;
public List<GameObject> lands = new List<GameObject>();
private int index;
//public Quaternion gunRotation;
public static bool gameOver;
public static int lives;
//UI FOR LIVES
public List<GameObject> lifeList; //HERE IS THE ARRAY I CREATED
public float time;
private float seconds;
private float startTime = 0f;
public static float score;
// Use this for initialization
void Start ()
{
gameOver = true;
NewGame();
}
// Update is called once per frame
void Update ()
{
if (!gameOver)
{
gameState ();
time = (int)(seconds += Time.deltaTime);
enemyProgression ();
}
else
{
CancelInvoke();
}
}
void FixedUpdate ()
{
}
void NewGame ()
{
time = startTime;
score = 0f;
Instantiate(lands[0], landSpawn, Quaternion.identity);
InvokeRepeating("spawnLand", 0f, 6.3f);
lives = 3;
Debug.Log(lifeList.Count); //SHOWS 3 CUZ I FILLED IT WITH GAMEOBJECTS IN INSPECTOR
Instantiate(player, playerSpawn, Quaternion.identity);
gameOver = false;
}
void gameState ()
{
if (lives <= 0)
{
gameOver = true;
endGame();
}
}
public void respawnPlayer ()
{
if (!gameOver)
{
Debug.Log(lifeList.Count); //SHOWS ZERO ANYWHERE IN respawnPlayer function.
if (lives > 0)
{
Instantiate (player, playerSpawn, Quaternion.identity);
}
}
}
private void spawnLand ()
{
index = Random.Range (0, lands.Count);
Instantiate(lands[index], landSpawn + (lands[index].transform.right * 32), Quaternion.identity);
}
private void endGame ()
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
private void enemyProgression ()
{
if (time >= 30)
{
}
}
}
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:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class AsteroidController : MonoBehaviour {
public GameManager gameManager;
private float xSpeed = -3f;
private float ySpeed = -3f;
// Use this for initialization
void Start ()
{
if (transform.position.x < 10)
{
xSpeed *= -1;
}
}
// Update is called once per frame
void Update ()
{
transform.Translate (xSpeed * Time.deltaTime, ySpeed * Time.deltaTime, transform.position.z, Camera.main.transform);
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Destroy (other.gameObject);
Destroy (gameObject);
GameManager.lives--;
gameManager.respawnPlayer();
}
}
}
Is it possible you have mistakenly created two instances of your GameManager component? One with the inspector filled out, one without.
Calling Destroy(gameObject); on line 34 before gameManger.respawnPlayer() might be the issue