Search Unity

GUI object error

Discussion in 'Scripting' started by scr33ner, Mar 2, 2015.

  1. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    188
    Hi all,

    I'm working on a health GUI bar & I need a little help fixing an object error that I'm getting.

    The error:
    NullReferenceException: Object reference not set to an instance of an object
    GuiTest.Update () (at Assets/scripts/GuiTest.cs:51)

    The code:
    Code (CSharp):
    1. /// <summary>
    2. /// GUI test.
    3. /// attach this to corresponding UI objects
    4. /// </summary>
    5.  
    6. using UnityEngine;
    7. using UnityEngine.UI;
    8. using System.Collections;
    9.  
    10. public class GuiTest : MonoBehaviour
    11. {
    12.  
    13.     private int startHealth = 100;
    14.  
    15.     public Text txtHealthString; //text field display
    16.  
    17.     public GameObject guiHealthBarObj; //health bar display
    18.  
    19.     public Object guiHealthBarImg; //health bar image
    20.  
    21.     private int testValue = 10;
    22.  
    23.     private int updatedValue = 0;
    24.     //private int newValue = 0;
    25.  
    26.     // Use this for initialization
    27.     void Awake ()
    28.     {
    29.  
    30.         //updatedValue.ToString();
    31.  
    32.         txtHealthString.text = "Start Health: " + startHealth;
    33.  
    34.         txtHealthString = GetComponent<Text>();
    35.         print("starting: " + startHealth);
    36.         print("updated value: " + updatedValue);
    37.  
    38.     }
    39.     void Update ()
    40.     {
    41.         //txtHealthString.text = startHealth.ToString ();
    42.      
    43.         //print (txtHealthString);
    44.         //updatedValue.ToString();
    45.         //startHealth.ToString();
    46.         //txtHealthString = GetComponent<Text>();
    47.  
    48.             if (Input.GetKeyUp ("up"))
    49.             {
    50.                 AddInt ();
    51.                 txtHealthString.text = updatedValue.ToString ();
    52.              
    53.                 print("updated value: " +updatedValue);
    54.             }
    55.          
    56.             if (Input.GetKeyUp ("down"))
    57.             {
    58.                 SubInt ();
    59.                 txtHealthString.text = updatedValue.ToString ();
    60.          
    61.                 //else{return;}
    62.             print("updated value: " +updatedValue);
    63.             }
    64.  
    65.  
    66.     }
    67.  
    68.     void AddInt()
    69.     {
    70.  
    71.         updatedValue = startHealth + testValue;
    72.         txtHealthString = GetComponent<Text>();
    73.         //print(updatedValue);
    74.     }
    75.     void SubInt()
    76.     {
    77.  
    78.         updatedValue = startHealth - testValue;
    79.         txtHealthString = GetComponent<Text>();
    80.         //print(updatedValue);
    81.     }
    82.     /*
    83.     void UpdateGraphic()
    84.     {
    85.         if (updatedValue == 75)
    86.         {
    87.             //guiTexture.texture = "";
    88.         }
    89.     }*/
    90.  
    91.  
    92.     // Update is called once per frame
    93.  
    94.     }
    95.  
    96.  
    Please help me figure out what I'm doing wrong.

    Thank you in advance
     
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Does txtHealthString have a text object assigned to it?
     
  3. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    188
    txtHealthString.png
    Yes it does...
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Question: If txtHealthString is assigned in the inspector, why do you keep on GetComponent'ing for it? Maybe the GetComponent is making it null during runtime
    Question 2: More than 1 instance of the script in the scene? eg on an object where there is no text component?
     
  5. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    188
    thanks hpjohn, got rid of the GetComponent & fixed the error...I suppose I was confused with implementing the code