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:
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:
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