- Home /
Problem is not reproducible or outdated
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 :)
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)
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.
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
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
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
So i take that as a reason to close the question as the problem is no longer relevant.
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>();