Search Unity

GUI Text doesn't change, provided error unknown

Discussion in 'Scripting' started by Anon1234, Nov 23, 2014.

  1. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ignition : MonoBehaviour {
    5.  
    6.     public GUIText hpDisplay;
    7.     public double health = 100.00;
    8.     // Use this for initialization
    9.     void Start() {
    10.         string hpDisplayReady = "HP: " + health;
    11.         hpDisplay.text = hpDisplayReady;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update() { }
    16.     void OnTriggerEnter(Collider firebox) {
    17.         if (firebox.gameObject.name == "Player") {
    18.         }
    19.     }
    20.     void OnTriggerExit(Collider firebox) {
    21.         if (firebox.gameObject.name == "Player") {
    22.             Debug.Log("HP decrease turndown.");
    23.         }
    24.     }
    25. }
    This is my code, it isn't worked out yet, but I need to work out the bugs before attempting to add new things. I wanted to display "HP: 100.00" or "HP: 100" on GUI text. When I run "the game", "Gui text" doesn't change to either of provided expected answers... the error I get, on the bar beneath and in-console too is:

    "UnassignedReferenceException: The variable hpDisplay of ignition has not been assigned.
    You probably need to assign the hpDisplay variable of the ignition script in the inspector.
    ignition.Start () (at Assets/ignition.cs:11)"
     
  2. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Most probably you do not assign the GUIText to your script.

    If you assigned the script to an object you must see to fields in the inspector (similar to the screenshot). Drag and Drop the GUIText object into the "slot" hpDisplay.
     

    Attached Files:

    Anon1234 likes this.
  3. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    Yep, I forgot to drop the box in the thingie, thank you Sir.