Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I dont understand whats wrong with this plz help!

Discussion in 'Scripting' started by beniboy2003, Nov 8, 2016.

  1. beniboy2003

    beniboy2003

    Joined:
    May 15, 2016
    Posts:
    19
    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<Image>();
    WaterBar = gameObject.GetComponent("Main Camera").transform.FindChild("Canvas").FindChild("WaterBar").GetComponent<Image>();
    HungerBar = gameObject.GetComponent("Main Camera").transform.FindChild("Canvas").FindChild("HungerBar").GetComponent<Image>();
    }

    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
     
    Last edited: Nov 8, 2016
  2. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    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.
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    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...

    Code (csharp):
    1. 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:
    Code (csharp):
    1.  
    2. Transform CanvasTrans = transform.FindChild("Canvas");
    3. if (CanvasTrans == null)
    4. {
    5.    Debug.Log("CanvasTrans was null");
    6.    return;
    7. }
    8. Transform WaterBarTrans = Canvas.FindChild("WaterBar");
    9. if (WaterBarTrans== null)
    10. {
    11.    Debug.Log("WaterBarTrans was null");
    12.    return;
    13. }
    14. Image WbImage = WaterBarTrans.GetComponent<Image>();
    15. if (WbImage == null)
    16. {
    17.    Debug.Log("WbImage was null");
    18.    return;
    19. }
    20.  
    21. WaterBar = WbImage;
    22. Debug.Log("Success!")
     
    Kiwasi likes this.
  4. beniboy2003

    beniboy2003

    Joined:
    May 15, 2016
    Posts:
    19
    this is where the error was:
    HealthBar.fillAmount = tmpHealth;
    WaterBar.fillAmount = tmpWater;
    HungerBar.fillAmount = tmpHunger;
     
  5. beniboy2003

    beniboy2003

    Joined:
    May 15, 2016
    Posts:
    19

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

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    Because one of the bar variables is null.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Just make the references public and then draw and drop in the inspector.
     
  8. beniboy2003

    beniboy2003

    Joined:
    May 15, 2016
    Posts:
    19
    do u mean like: public float tmpHealth = 1;
     
  9. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Like:
    Code (csharp):
    1. public Image HealthBar;
    2. public Image WaterBar;
    3. 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.
     
    Kiwasi likes this.