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 /
This question was closed May 24, 2016 at 01:12 PM by Bunny83 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by A_Random_Guy · May 22, 2016 at 11:31 AM · uibuttontextnullreferenceexception

Null reference when I already assign a value? (UI Button and Text)

Hey guys! I am making a cheesy 2D platformer, and for some reason, it is saying that the text fields are null, which is odd, because I specifically give them values. Here is the code:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class LevelUp : MonoBehaviour {

 //The text stating that the player leveled up

 public GameObject levelText;

 //The button GameObject that the user can click

 public Button[] BubbleText = new Button[2];

 //The text on the buttons

 public Text[] text = new Text[2];

 //The string of text options that the user can pick from

 public string[][] RogueLvl = new string[3][];

 // Use this for initialization
 void Start () {

     //Some bug testing
     //This doesn't show up, so BubbleText[0] is not null

     if(BubbleText[0]==null)
     {
         print("BubbleText[0] is null"); 
     }

     //Assigning the Text object to the text on the buttons

     text[0] = BubbleText[0].gameObject.GetComponent<Text>();
     text[1] = BubbleText[1].gameObject.GetComponent<Text>();

     //Temporary statements, to be later upgraded with actual sentences

     RogueLvl[2][0] = "Statement1";
     RogueLvl[2][1] = "Statement2";
  
 }
 
 // Update is called once per frame
 void Update () {

 }


 void Rogue(int l)
 {
     for(int i =0; i<RogueLvl.Length; i++)
     {
         //Enabling the gameObjects in the scene

         BubbleText[i].gameObject.SetActive(true);

         //Some bug testing, the first statement Returns 2, the second one returns 0;

         print("L is " + l);
         print("I is " + i);

         //If text[i] is null, then  this print statement fires
         //THIS STATEMENT IS TRUE, so it prints "Text [I] is null"

         if(text[i]==null)
         {
             print("Text "+i+" is null");
         }

         //Assigning the statement to the text field
         //This is the line where I get the error

         text[i].text = RogueLvl[l][i]+"";
     }
 }

Any help would be awesome, and thanks in advance everyone :)

Comment
Add comment · Show 6
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 saschandroid · May 23, 2016 at 09:54 AM 0
Share

RogueLvl.Length (in line 48) gives you the number of all elements in the 2-dimensional array. So if your array is of size [3][2] (for example) you are trying to access the element RogueLvl[j][5] at the end of your for-loop (which doesn't exist). Try changing

 RogueLvl.Length

to

 RogueLvl.GetLength(1)



avatar image troien saschandroid · May 23, 2016 at 10:16 AM 0
Share

Although you are right this will throw an error, the provided code never gets to this point as it throws an error before 'i' gets out of range. Also, this will throw an IndexOutOfRange exception and not a NullReference as described.

avatar image Bunny83 saschandroid · May 23, 2016 at 10:26 AM 1
Share

That's not a two dimensional array, it's a jagged array. $$anonymous$$ultidimensional arrays look like this:

 string[,] RogueLvl = new string[3, 5];

You can't omit the size of a dimension of a multidimensional array. Jagged arrays are different. It's basically an array of arrays. If you use jagged arrays you have to create the inner arrays manually.

This line:

 public string[][] RogueLvl = new string[3][];

will only create the "outer array" with 3 elements. Each element is a "string[]" which however is null in the beginning. You would need to create an assign each element array manually:

 for(int i = 0; i < RogueLvl.Length; i++)
 {
     RogueLvl[i] = new string[5]; // define size of the "second" dimension
 }

Jagged arrays have more overhead as they consist of many seperate arrays and access to an element need two lookups. However they allow to have different array sizes in the second dimension or to omit a nested array:

 RogueLvl[0] = new string[5]; 
 RogueLvl[1] = new string[2]; 
 RogueLvl[2] = null; 

 RogueLvl[0][4]; // valid
 RogueLvl[1][1]; // valid
 RogueLvl[1][4]; // invalid: out of bounds
 RogueLvl[2][0]; // invalid: null ref exception
avatar image troien Bunny83 · May 23, 2016 at 10:57 AM 0
Share

Whoops,you're right :p

To add to the original comment of @saschandroid , in the for loop ins$$anonymous$$d of using Roguelvl.Length, use Roguelvl[l].Length. For cases when Roguelvl.Length and Roguelvl[l].Length are not equal :p

avatar image A_Random_Guy · May 23, 2016 at 10:46 PM 0
Share

First off, thanks for the answers everyone, I really appreciate it!

The issue was that the button did not have a text field, so I had to move some variables around there. Thanks a lot Troien for that suggestion :P

The other issue was with the array. Like Bunny83 said, it wasn't 2 dimensional, so I had to make it 2 dimensional and assign the all the values up until the one I wanted to access (if that makes any sense).

Anyways, long story short I fixed the errors with the help of you guys, y'all are awesome :D

avatar image Bunny83 A_Random_Guy · May 24, 2016 at 01:12 PM 0
Share

So i take that as a reason to close the question as the problem is no longer relevant.

1 Reply

  • Sort: 
avatar image
1

Answer by troien · May 22, 2016 at 12:09 PM

It would help if you tell where the error occurs (In the question, I now see that you did it in the comments :p).

Also, you are probably setting the values to null yourself in here:

 text[0] = BubbleText[0].gameObject.GetComponent<Text>();

When there is no Text component on your BubbleText[0] then this will set text[0] to null.

And since your BubbleText[0] is a Button Component, I'm assuming the Text component is on a child of the button component instead of on the same GameObject as the Button component. So changing it to this should work:

 text[0] = BubbleText[0].gameObject.GetComponentInChildren<Text>();
Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Dynamically generate buttons from a single text component 0 Answers

Clicking a button will display that buttons text in a seperate text-field 1 Answer

Change the text of a UI button on clicking it? 1 Answer

How to lock UI elements position? 1 Answer

Force Unity UI element to refresh/update? 4 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