Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Object Reference Error

Discussion in 'Scripting' started by Chrotesque, Apr 27, 2021.

  1. Chrotesque

    Chrotesque

    Joined:
    Sep 7, 2019
    Posts:
    11
    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:
    Code (CSharp):
    1. public class HealthSystem : MonoBehaviour {
    2.  
    3.  
    4.     public float healthMax;
    5.     float healthCurrent;
    6.     public GameObject healthBarPrefab;
    7.  
    8.     HealthBar myHealthBar;
    9.    
    10.     // called before the first frame update
    11.     void Start() {
    12.         healthCurrent = healthMax;
    13.         GameObject healthBarObject = Instantiate(healthBarPrefab, References.UI.transform);
    References UI is this references.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public static class References {
    6.  
    7.     public static GameObject playerShip;
    8.     public static GameObject UI;
    9. }
    healthBarPrefab is also dragged in:
    https://imgur.com/iul9cKb

    And 36:
    Code (CSharp):
    1. void Update() {
    2.         myHealthBar.ShowHealthFraction(healthCurrent / healthMax);
    ShowHealthFraction comes from HealthBar.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HealthBar : MonoBehaviour {
    7.  
    8.     public Image filledPart;
    9.  
    10.     public void ShowHealthFraction(float fraction) {
    11.         filledPart.rectTransform.localScale = new Vector3(fraction, 1, 1);
    12.     }
    13.  
    14. }
    Also all correctly set up in Unity itself:
    https://imgur.com/UcTqFS3

    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
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,760
    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://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

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

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Code (CSharp):
    1.         GameObject healthBarObject = Instantiate(healthBarPrefab, References.UI.transform);
    2.  
    "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.
     
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455

    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.
    Code (CSharp):
    1. myHeatlhBar = FindObjectOfType<HealthBar>();
     
  5. Chrotesque

    Chrotesque

    Joined:
    Sep 7, 2019
    Posts:
    11
    To
    &
    That would be this here:
    Code (CSharp):
    1. public class HealthSystem : MonoBehaviour {
    2.  
    3.  
    4.     public float healthMax;
    5.     float healthCurrent;
    6.     public GameObject healthBarPrefab;
    7.  
    8.     HealthBar myHealthBar;
    9.     // called before the first frame update
    10.     void Start() {
    11.         healthCurrent = healthMax;
    12.         GameObject healthBarObject = Instantiate(healthBarPrefab, References.UI.transform); // fix me
    13.         myHealthBar = healthBarObject.GetComponent<HealthBar>();
    14.     }
    Unless I misunderstand something of course.

    The References.UI thing looks like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public static class References {
    6.  
    7.     public static GameObject playerShip;
    8.     public static GameObject UI;
    9. }
    10.  
    And the UI object itself (which is a canvas) has the script UIbehaviour.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class UIbehaviour : MonoBehaviour {
    6.  
    7.  
    8.  
    9.     // called before the first frame update
    10.     void Start() {
    11.         References.UI = gameObject;
    12.     }
    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
    I'll go through that now, as it takes a bit more time to understand. Thank you in anyway!
     
  6. Chrotesque

    Chrotesque

    Joined:
    Sep 7, 2019
    Posts:
    11
    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:
    Code (CSharp):
    1.     void Start() {
    2.         References.canvas = gameObject;
    3.     }
    became
    Code (CSharp):
    1.     void Awake() {
    2.         References.canvas = gameObject;
    3.     }
    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
     
  7. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455

    Ye thats it .. myHealthBar = healthBarObject.GetComponent<HealthBar>(); If your health bar is attached on same GO in editor.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,760
    Make sure you understand how such timing works!

    Here is some timing diagram help:

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

    Chrotesque

    Joined:
    Sep 7, 2019
    Posts:
    11
    Kurt-Dekker likes this.