is my enemy selector null reference a problem?
Hello Everyone,
I'm new to Unity but I'm getting along so far from the great tutorials and questions that have already been answered. I have created the script below to display the health of enemy or companion characters but which character is dependent on a Raycast from the camera (in the direction of the player) and is not initialised in void Start(). I get the "NullReferenceException" error from line 40 but it is still playable and works as intended. I just think there must be a way to code this better to avoid the error.
CharacterHealthManager playerHealth;
CharacterHealthManager selectedHealth;
CanvasGroup cg;
public Slider healthBar;
public Slider selectedHealthBar;
public Image selectedHealthBarFill;
public Text hpText;
public Text selectedHPText;
public Text objName;
// Use this for initialization
void Start () {
playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterHealthManager>();
cg = GetComponentInChildren<CanvasGroup>();
}
// Update is called once per frame
void Update() {
healthBar.maxValue = playerHealth.characterMaxHealth;
healthBar.value = playerHealth.characterCurrentHealth;
hpText.text = "HP: " + playerHealth.characterCurrentHealth + "/" + playerHealth.characterMaxHealth;
DisplaySelectedHealth();
}
void DisplaySelectedHealth()
{
if (GameObject.FindGameObjectWithTag("MainCamera").GetComponent<MouseManager>().selectedObject.GetComponent<CharacterHealthManager>() != null)
{
cg.alpha = 1;
selectedHealth = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<MouseManager>().selectedObject.GetComponent<CharacterHealthManager>();
if (selectedHealth.CompareTag("Ally"))
{
selectedHealthBarFill.color = Color.magenta;
//TODO add Color.Lerp for smooth color transition.
}
else if (selectedHealth.CompareTag("Enemy"))
{
selectedHealthBarFill.color = Color.black;
}
objName.text = ("" + selectedHealth.name);
selectedHealthBar.maxValue = selectedHealth.characterMaxHealth;
selectedHealthBar.value = selectedHealth.characterCurrentHealth;
selectedHPText.text = "HP: " + selectedHealth.characterCurrentHealth + "/" + selectedHealth.characterMaxHealth;
}
else
{
cg.alpha = 0;
}
}
}
Firstly, if you don't post your script starting from the very first line, we can't tell which line Line 40 is unless you point it out to us with an inline comment.
Secondly, Unity is pretty robust and won't crash from most runtime errors, but having an error like this still means that things won't work the way you intend them to if you don't fix it.
Thirdly, a null reference error means you're trying to reference a member of an instance that doesn't exist, i.e. you're accessing someVariable.someFunctionOrVariable
when someVariable is null.
Find Line 40, figure out what's null in that line that's not supposed to be null, figure out WHY it's null when it's not supposed to be null, and then you'll be able to fix your error.
EDIT: Fourthly, this is unrelated, but it's generally Not Good to find an object by string in every Update, and Doubly Not Good to search for the same object several times in the same Update step. Best practice for when you have to search for an object by name or tag is to do it once (typically in the Start event) and save a reference to that object in a variable.
Answer by Socapex · Mar 15, 2017 at 05:12 AM
if (thing != null) {
DoSomething();
}
;)
Ok, if it isn't ever supposed to be null, you can use asserts. This will at least warn you during development time.
using UnityEngine.Assertions;
Assert.IsNotNull(thingy);
Finally, yes it matters. Exceptions will rewind your stack and leave you in undefined state. If it continues working, that is pure luck. You need to fix it, most often you can just make sure it isn't null.