Hey guys,
This one’s really testing my sanity, so I’m hoping you can help. I’m getting a null error from one of my scripts when I call a variable from another script, and I can’t figure out what the reason is.
Now I have a script called cs_GM that is attached to an empty called GameManager, which holds static references to several scripts in my game. I have another script called cs_UIManager which displays all of my UI, which is attached to the UI canvas. And the script I’m having the problem with, called cs_LivesDisplay, is also attached to the canvas that cs_UIManager is attached to. In it, it calls a variable called currentLives from a script called cs_LivesManager, which is attached to the GameManager object.
The problem I’m having is that when it calls that variable, it returns null, and I can’t figure out why it’s returning null. If I copy/paste the line that calls the variable to cs_UIManager, it works fine from there.
I’m sure I’m missing something simple. Can someone please point it out to me?
Here are the relevant scripts. Sorry to leave so many, but I don’t want to leave out something that might be the problem. Thanks.
cs_GM
using UnityEngine;
using System.Collections;
public class cs_GM : MonoBehaviour {
public static GameObject Player;
public static cs_PlayerMove ref_cs_PlayerMove;
public static cs_PlayerDamage ref_cs_PlayerDamage;
public static cs_PlayerInventory ref_cs_PlayerInventory;
public static cs_SpawnManager ref_cs_SpawnManager;
public static cs_UIManager ref_cs_UIManager;
public static cs_ItemHUD ref_cs_ItemHUD;
public static cs_TimeManager ref_cs_TimeManager;
public static cs_ScoreManager ref_cs_ScoreManager;
public static cs_PlayerRespawn ref_cs_PlayerRespawn;
public static cs_CameraFollow2D ref_cs_CameraFollow2D;
public static cs_LivesManager ref_cs_LivesManager;
public static cs_ReachExit ref_cs_ReachExit;
public static debugTest ref_debugTest;
}
cs_UIManager
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class cs_UIManager : MonoBehaviour {
public Text healthAmount;
public Image healthBar;
public Image timeBar;
public Text enemiesAmount;
public Text scoreAmount;
public Text timeElapsedAmount;
public Text timeLeftAmount;
public Text livesAmount;
public Text bombsAmount;
void Start ()
{
cs_GM.ref_cs_UIManager = this; // allows this static script to be accessed by the other scripts
DisplayUI();
}
void Update ()
{
DisplayUI();
}
void DisplayUI()
{
healthAmount.text = cs_GM.ref_cs_PlayerDamage.playerHealth.ToString("0");
healthBar.fillAmount = cs_GM.ref_cs_PlayerDamage.playerHealth * 0.01f;
timeBar.fillAmount = cs_GM.ref_cs_TimeManager.timeLeft * 0.05f;
bombsAmount.text = cs_GM.ref_cs_PlayerInventory.hasBomb.ToString("0");
timeElapsedAmount.text = cs_GM.ref_cs_TimeManager.timeElapsed.ToString("000");
timeLeftAmount.text = cs_GM.ref_cs_TimeManager.timeLeft.ToString("00");
scoreAmount.text = cs_GM.ref_cs_ScoreManager.score.ToString("000");
livesAmount.text = cs_GM.ref_cs_LivesManager.currentLives.ToString("0");
Debug.Log(cs_GM.ref_cs_LivesManager.currentLives); // **** DOES NOT RETURN NULL ****
}
}
cs_LivesDisplay, in which I am getting the error
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cs_LivesDisplay : MonoBehaviour
{
void Start ()
{
Debug.Log(cs_GM.ref_cs_LivesManager.currentLives); // ****RETURNS NULL****
}
}
cs_LivesManager
using UnityEngine;
using System.Collections;
public class cs_LivesManager : MonoBehaviour
{
public int currentLives = 3; // the number of lives the player has
void Start ()
{
cs_GM.ref_cs_LivesManager = this; // allows this static script to be accessed by the other scripts
}
void Update ()
{
if(currentLives == 0) // if player has no more lives
{
Debug.Log("Game Over"); // call Game Over function here
}
}
public void GiveLife()
{
currentLives++; // increments lifeCounter by +1
}
public void TakeLife()
{
currentLives--; // increments lifeCounter by -1
}
}