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;
        }

    }
}

if (thing != null) {
DoSomething();
}

:wink:

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.