New to this scene and I’m following a tutorial, but I can’t figure this out. The error I’m getting is:
“NullReferenceException: Object reference not set to an instance of an object
Player.Start () (at Assets/Scripts/Player.cs:13)”
Edit: I’m attaching the Stats.cs to a healthbar image if that helps. Trying to get the fillAmount to match the percentage of how much health the player has.
Here’s Player.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : Character {
private Stats Health;
private float initHealth = 100;
// Use this for initialization
protected override void Start () {
Health.Initialize (initHealth, initHealth);
base.Start ();
}
// Update is called once per frame
protected override void Update () {
base.Update ();
}
}
And Stats.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Stats : MonoBehaviour {
private Image content;
private float currentFill;
private float maxValue;
public float MaxValue { get; set; }
private float currentValue;
public float CurrentValue {
get {
return currentValue;
}
set {
if (value > MaxValue) {
currentValue = MaxValue;
} else if (value < MaxValue) {
currentValue = 0;
} else {
currentValue = value;
}
currentFill = currentValue / MaxValue;
}
}
// Use this for initialization
void Start () {
content = GetComponent<Image>();
}
// Update is called once per frame
void Update () {
Debug.Log (CurrentValue);
content.fillAmount = currentFill;
}
public void Initialize (float currentValue, float maxValue){
CurrentValue = currentValue;
MaxValue = maxValue;
}
}