Object Reference Error (838521)

Hey all!

I’m rather new to this and am following a sort of tutorial series

Replicating the code seen on the video while making a few changes along the way to fit my own style / needs.

I’m running into 2 of these babies: “Object reference not set to an instance of an object HealthSystem.Start () (at Assets/Scripts/HealthSystem.cs:17)”
&
“Object reference not set to an instance of an object HealthSystem.Update () (at Assets/Scripts/HealthSystem.cs:36)”

The code up to 17:

public class HealthSystem : MonoBehaviour {


    public float healthMax;
    float healthCurrent;
    public GameObject healthBarPrefab;

    HealthBar myHealthBar;
   
    // called before the first frame update
    void Start() {
        healthCurrent = healthMax;
        GameObject healthBarObject = Instantiate(healthBarPrefab, References.UI.transform);

References UI is this references.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class References {

    public static GameObject playerShip;
    public static GameObject UI;
}

healthBarPrefab is also dragged in:
Imgur

And 36:

void Update() {
        myHealthBar.ShowHealthFraction(healthCurrent / healthMax);

ShowHealthFraction comes from HealthBar.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthBar : MonoBehaviour {

    public Image filledPart;

    public void ShowHealthFraction(float fraction) {
        filledPart.rectTransform.localScale = new Vector3(fraction, 1, 1);
    }

}

Also all correctly set up in Unity itself:
Imgur

The tutorial series works in Unity 2018.4.19f1 while I decided to go with 2020.3.4f1. I’ve been sitting in front of these error messages for like 2 days now and I’m still none the wiser. Initially (believe it or not) the healthbars worked FINE on 2/3 objects in my scene. But I couldn’t figure out why it wouldn’t work for the player, thus saved and restarted the project. Now I was down 3/3 that wouldn’t work.

I understand what the error messages try to say in principle, but I fail to understand why they claim what they claim - if that makes any sense.

If anybody can take a look at this, I’m utterly helpless at this point and either blind or truly don’t understand some of the basics (likely).

Kind regards,
Chrotesque

Those babies are the most common babies of all.

And the answer to those babies is always the same… ALWAYS. It is the single most common babies error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

        GameObject healthBarObject = Instantiate(healthBarPrefab, References.UI.transform);

“References.UI” is a static variable. How are you assigning anything to this variable? If you do not assign something to it, than it will be null, and you will get a nullreferenceexception.

Where are you assigning anything to “myHealthBar”? Like above, it will be null unless you assign something to it.

You didnt set your reference for HealthBar just put this int Start() and make sure that HealthBar script is attached on some GO in Unity.myHeatlhBar = FindObjectOfType<HealthBar>();

To [quote=“bobisgod234, post:3, topic: 838521, username:bobisgod234”]

        GameObject healthBarObject = Instantiate(healthBarPrefab, References.UI.transform);

“References.UI” is a static variable. How are you assigning anything to this variable? If you do not assign something to it, than it will be null, and you will get a nullreferenceexception.

Where are you assigning anything to “myHealthBar”? Like above, it will be null unless you assign something to it.
[/quote]
&

That would be this here:

public class HealthSystem : MonoBehaviour {


    public float healthMax;
    float healthCurrent;
    public GameObject healthBarPrefab;

    HealthBar myHealthBar;
    // called before the first frame update
    void Start() {
        healthCurrent = healthMax;
        GameObject healthBarObject = Instantiate(healthBarPrefab, References.UI.transform); // fix me
        myHealthBar = healthBarObject.GetComponent<HealthBar>();
    }

Unless I misunderstand something of course.

The References.UI thing looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class References {

    public static GameObject playerShip;
    public static GameObject UI;
}

And the UI object itself (which is a canvas) has the script UIbehaviour.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIbehaviour : MonoBehaviour {

 

    // called before the first frame update
    void Start() {
        References.UI = gameObject;
    }

So I think the UI stuff is solid, all it does say is: I’m the UI object so that other objects like the healthbar can refer to it “globally”. I’m sure there is a better of way of doing this but it should not cause issues, right?

In regards to [quote=“Kurt-Dekker, post:2, topic: 838521, username:Kurt-Dekker”]
Those babies are the most common babies of all.

And the answer to those babies is always the same… ALWAYS. It is the single most common babies error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.
[/quote]

I’ll go through that now, as it takes a bit more time to understand. Thank you in anyway!

Aight so removing healthbar related stuff and following the tutorial once again, I’ve noticed something I must have slept through before which also perfectly explains the behaviour I saw, namely that the healthbars worked at one point and no longer after a restart and then sometimes afterwards despite not having changed anything in the code - it felt random.

And it’s also something I didn’t fully consider - the order of things, when things are being declared and the issue simply was that I referred to the UI while references.UI didn’t run yet, apparently sometimes (but rarely) it did first but most of the time it didn’t and thus it was referring to nothing.

Just for clarity, this:

    void Start() {
        References.canvas = gameObject;
    }

became

    void Awake() {
        References.canvas = gameObject;
    }

Seemingly just to make sure that creating the references happens before the start, before objects start referring to it in start or later stages.

Thank you all <3

Ye thats it … myHealthBar = healthBarObject.GetComponent(); If your health bar is attached on same GO in editor.

Make sure you understand how such timing works!

Here is some timing diagram help:

https://docs.unity3d.com/Manual/ExecutionOrder.html

Awesome, thank you!