I'm getting a Null reference exception on an array of strings (I think). So I put in a statement to check for null on my array, and it still gets thrown. I'm using Unity so my debugging tools aren't great. Is there anyway I can get Unity or Visual Studio to spit out exactly what is null here? I'm kind of at a loss. I'm fairly certain this array has been instantiated, since one, I know it isn't null due to my statement, and two, this code runs multiple times just fine. It is only after I fill up choices and then remove choices that I get the Null Reference. I'm using an engine (Dialoguer), but I'm hoping it isn't related.
--EDIT--
After a little debugging, I found out the following. Removing the choices.Length line removed the NRE the following code works as is, assuming that the number of input choices is two:
for (int i = 0; i < 2/*choices.Length*/; i++)
{
Debug.Log(choices[i]);
if (GUI.Button(new Rect(10, 220 + (40 * i), 200, 30), choices[i]))
{
Dialoguer.ContinueDialogue(i);
}
}
The code correctly prints the name of the two strings in choices[i]. So what I'm reading from this is that my array has elements, and is not null, but the Array.Length property is returning null for some reason. Is that correct?
Thanks for reading!
if(choices != null && choices.Length > 0)
{
for (int i = 0; i < choices.Length; i++) //NRE is thrown here.
{
if (GUI.Button(new Rect(10, 220 + (40 * i), 200, 30), choices[i]))
{
Dialoguer.ContinueDialogue(i);
}
}
}
Full code (new):
using UnityEngine;
using System.Collections;
public class DialoguerGUI : MonoBehaviour {
private bool isShowing;
private string text;
private string[] choices;
// Use this for initialization
void Start() {
choices = new string[19];
Dialoguer.events.onStarted += onStarted;
Dialoguer.events.onEnded += onEnded;
Dialoguer.events.onTextPhase += onTextPhase;
}
void OnGUI()
{
if (!isShowing)
return;
GUI.Box(new Rect(10, 10, 200, 150), text);
if (choices == null)
{
if (GUI.Button(new Rect(10, 220, 200, 30), "continue"))
{
Dialoguer.ContinueDialogue();
}
} else
{
for (int i = 0; i < 2/*choices.Length*/; i++)
{
Debug.Log(choices[i]);
if (GUI.Button(new Rect(10, 220 + (40 * i), 200, 30), choices[i]))
{
Dialoguer.ContinueDialogue(i);
}
}
}
}
// Update is called once per frame
void Update() {
}
private void onStarted()
{
isShowing = true;
}
private void onEnded()
{
isShowing = false;
}
private void onTextPhase(DialoguerTextData data)
{
text = data.text;
choices = data.choices;
}
}
Full Code (old):
using UnityEngine;
using System.Collections;
public class DialoguerGUI : MonoBehaviour {
private bool isShowing;
private string text;
private string[] choices;
// Use this for initialization
void Start() {
Dialoguer.events.onStarted += onStarted;
Dialoguer.events.onEnded += onEnded;
Dialoguer.events.onTextPhase += onTextPhase;
}
void OnGUI()
{
if (!isShowing)
return;
GUI.Box(new Rect(10, 10, 200, 150), text);
if (choices == null)
{
if (GUI.Button(new Rect(10, 220, 200, 30), "continue"))
{
Dialoguer.ContinueDialogue();
}
} else
{
if(choices != null && choices.Length > 0)
{
for (int i = 0; i < choices.Length; i++)
{
if (GUI.Button(new Rect(10, 220 + (40 * i), 200, 30), choices[i]))
{
Dialoguer.ContinueDialogue(i);
}
}
}
}
}
// Update is called once per frame
void Update() {
}
private void onStarted()
{
isShowing = true;
}
private void onEnded()
{
isShowing = false;
}
private void onTextPhase(DialoguerTextData data)
{
text = data.text;
choices = data.choices;
}
}