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.
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!")
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.