I dont understand whats wrong with this plz help!

using UnityEngine;
using System.Collections;
using Image=UnityEngine.UI.Image;
using System;

public class health : MonoBehaviour {
Image HealthBar;
Image WaterBar;
Image HungerBar;
float tmpHealth = 1f;
float tmpHunger = 0.5f;
float tmpWater = 1f;

void Start () {
HealthBar = gameObject.GetComponent(“Main Camera”).transform.FindChild(“Canvas”).FindChild(“HealthBar”).GetComponent();
WaterBar = gameObject.GetComponent(“Main Camera”).transform.FindChild(“Canvas”).FindChild(“WaterBar”).GetComponent();
HungerBar = gameObject.GetComponent(“Main Camera”).transform.FindChild(“Canvas”).FindChild(“HungerBar”).GetComponent();
}

void Update () {
if (Input.GetKeyDown(“1”))
{
tmpHunger += 0.1f;
}

HealthBar.fillAmount = tmpHealth;
WaterBar.fillAmount = tmpWater;
HungerBar.fillAmount = tmpHunger;
}
}

error:
object reference is not set to an instance of an object

Please use code tags.
Also provide the full error message (with line number, highlighting which line is it won’t hurt either), as right now any of those could be the culprit.

PS. There is a lot wrong with this script, but let’s start with fixing the compiler error.

Your start routine couldn’t find something.

A bad practice is stacking extensions on to something when you are not guaranteeing that the previous component is not null. For instance…

gameObject.GetComponent("Main Camera").transform.FindChild("Canvas").FindChild("WaterBar").GetComponent<Image>();

This is really bad. All of the code is dependent on the previous component not being null and you have zero checks to verify that. You’re saying that the component running this also has another component called “Main Camera” (it probably doesn’t), then for some reason you ignore that and go straight to the transform variable (which is the same as just doing ‘gameObject.transform’ or ‘transform’) and finding a child called “Canvas” (which probably isnt there since the first GetComponent likely failed), and continue this bad practice into several more Gets and Finds.

Break it into parts and try this:

Transform CanvasTrans = transform.FindChild("Canvas");
if (CanvasTrans == null) 
{
   Debug.Log("CanvasTrans was null");
   return;
}
Transform WaterBarTrans = Canvas.FindChild("WaterBar");
if (WaterBarTrans== null) 
{
   Debug.Log("WaterBarTrans was null");
   return;
}
Image WbImage = WaterBarTrans.GetComponent<Image>();
if (WbImage == null)
{
   Debug.Log("WbImage was null");
   return;
}

WaterBar = WbImage;
Debug.Log("Success!")
1 Like

this is where the error was:
HealthBar.fillAmount = tmpHealth;
WaterBar.fillAmount = tmpWater;
HungerBar.fillAmount = tmpHunger;

this is where the error was:
HealthBar.fillAmount = tmpHealth;
WaterBar.fillAmount = tmpWater;
HungerBar.fillAmount = tmpHunger;

Because one of the bar variables is null.

Just make the references public and then draw and drop in the inspector.

do u mean like: public float tmpHealth = 1;

Like:

public Image HealthBar;
public Image WaterBar;
public Image HungerBar;

And then just assign those in the editor.

Also, I’m pretty sure that what you’re intending to do with “Main Camera” is GameObject.Find, not GetComponent, but that’s a pretty fragile way to access it anyway.

1 Like