null reference error on player ui script
hello there its been a while since i posted here ok so my problem is that my playerui script is giving me a NRE on lines 39,65 and 66 which is strange because as you will see in the picture shown there are set correctly in the inspector what am i doing wrong here is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerUI : MonoBehaviour
{
public Image SleepIcon;
public Image ToiletIcon;
public Image Fader;
public Image HealthBar;
public Image HungerBar;
public static Survivor S;
public static PlayerStats P;
Animator FadeAnim;
Animator SleepyAnim;
Animator NeedsToGoAnim;
int FadeHash = Animator.StringToHash ("fade");
int SleepyHash = Animator.StringToHash ("sleepy");
int NeedsToGoHash = Animator.StringToHash ("needstogo");
// Use this for initialization
void Start ()
{
FadeAnim = Fader.GetComponent<Animator> ();
SleepyAnim = SleepIcon.GetComponent<Animator> ();
NeedsToGoAnim = ToiletIcon.GetComponent<Animator> ();
SleepIcon.enabled = false;
ToiletIcon.enabled = false;
Fader.enabled = false;
}
void Update ()
{
ChangeUIBars ();
}
public void PlayAnim (int AnimHash)
{
if (AnimHash == 0) {
Fader.enabled = true;
FadeAnim.SetTrigger (FadeHash);
} else if (AnimHash == 1) {
SleepIcon.enabled = true;
SleepyAnim.SetTrigger (SleepyHash);
} else {
if (AnimHash == 2) {
ToiletIcon.enabled = true;
NeedsToGoAnim.SetTrigger (NeedsToGoHash);
}else{
SleepIcon.enabled = false;
ToiletIcon.enabled = false;
Fader.enabled = false;
}
}
}
public void ChangeUIBars ()
{
HealthBar.fillAmount = S.health/P.Health;
HungerBar.fillAmount = S.hunger/P.Hunger;
}
}
and here is the pic link text
ok so after looking again its safe to say that line 39 is not relevant to this as it calls 65 and 66
are you trying to make Survivor & PlayerStats a static scripts if you did that you don't need a reference to call them .
no i wasn't, thanks for pointing that out, however what would that have to do with NREs ?
so if they are not a static classes then you need to find the object that they attached to :
GameObject obj;
Survivor S;
PlayerStats P;
void Start(){
obj = GameObject.Find("the name of object that Survivor class is ittached to");
if(obj != null)
S = obj.GetComponent<Survivor>();
//if they are attached to the same object
P = obj.GetComponent<PlayerStats>();
// if not let me know
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612074111im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Having a issue with a NullReferenceException 1 Answer
Not sure what this error is telling/asking me to do? 1 Answer
Using TextMeshProUGUI variable in a static function is returning a NullReferenceException 0 Answers
Unity Bug? null reference exception, Only its not null... 0 Answers
Object reference not set message, directly after instantiation 1 Answer