null reference exception with text
I'm trying to display a string out of an array through the ui. It displays it correctly once and then throws up a null reference exception and I have no idea why. Everything I've looked at says that this is the correct implementation but it just refuses to work. Please help. I'll post the code below the exception points to the set_text function at the top.
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class item_inventory : MonoBehaviour { public Text item_text; public AudioClip sound; public int item_num = 3; private string[] inventory = new string[4]; public string[] item_list = {"brake", "item2", "item3", "item4",}; //these are temporary names jsut be sure to propogate any changes you make private int inventory_position = 0; // Use this for initialization void Start () { set_text(); }
public void set_text()
{
string temp = inventory[inventory_position];
if(temp == null)
{
temp = "none";
}
item_text.text = "item: " + temp + " position: " + inventory_position.ToString();
}
public void add_item(){
this.gameObject.GetComponent<AudioSource> ().PlayOneShot (sound);
int i = 0;
foreach (string item in inventory) {
if (item == null) {
int num = Random.Range (0, item_num);
string name = item_list [num];
inventory[i] = name;
Debug.Log ("added" + name + "to position" + i.ToString ());
inventory_position = i;
set_text();
break;
}
i++;
}
}
public void add_brake(){
int i = 0;
foreach (string item in inventory) {
if (item == null) {
inventory [i] = "brake";
Debug.Log ("added brake at position" + i.ToString ());
inventory_position = i;
break;
}
i++;
}
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("space")) {
//this will cycle through the inventory feel free to replace the input with aything you want.
//right now you can only traverse the inventory one way with it wrapping around to the begining.
//this is also where you would put the code for displaying the current item on the hud.
Debug.Log(inventory_position);
if(inventory_position == 3){
inventory_position = 0;
}
else{
inventory_position ++;
}
Debug.Log(inventory_position);
string item = inventory[inventory_position];
Debug.Log (item + "at" + inventory_position);
set_text();
}
if (Input.GetKeyDown ("a")) {
//this is activating the item
string item = inventory [inventory_position];
if (item == "brake") {
Debug.Log ("brake");
StartCoroutine (this.gameObject.GetComponent<tempSlow>().temp_slow(3, 1));
//effeect of item1 goes here. The rest of the items effects should go in the same spot.
//again we could instead pull from some other object that has all of the item scripts
}
else if (item == "item2") {
Debug.Log ("item2");
}
else if (item == "item3") {
Debug.Log ("item3");
}
else if (item == "item4") {
Debug.Log ("item4");
}
else if(item == null){
Debug.Log ("no action");
}
inventory[inventory_position] = null;
set_text();
}
}
}
I eneded up solving the problem by attaching a script to the text component of the ui that referenced this script, but I'm still interested if there is another way of doing it.
Your answer
Follow this Question
Related Questions
uGUI Text draw calls 1 Answer
Show a text in the position of a GameObject 0 Answers