Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by bigred0603 · Mar 19, 2016 at 10:57 PM · uitext

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();
     }
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bigred0603 · Mar 20, 2016 at 02:08 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

65 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

uGUI Text draw calls 1 Answer

Show a text in the position of a GameObject 0 Answers

Can not properly display the score on pong game 0 Answers

I have a score displayed constantly in my game, but I can't get it to work for my game-complete screen? Help? 0 Answers

UI Text rendering in front and behind planes 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges