- Home /
Attempt at creating an array of text files results in different errors
Hello. I've been attempting to create an array based off of text objects and display them as buttons on screen. However when creating the buttons I would get a null reference exception error and I dont understand why this is happening.
Debug.log correctly indicates the current position in the array but upon displaying them within the scene Unity gives off an error. Any idea on what I'm doing wrong and any way to fix this? I tried to use a List instead but I wouldn't be able to load in the text files.
public class QuestionPackSelection : MonoBehaviour {
public TextAsset[] texts;
GUIStyle PackGui;
public int numberofquestionpacks;
// Use this for initialization
void Start () {
texts = new TextAsset[numberofquestionpacks];
texts = Resources.LoadAll<TextAsset>("QuestionPacks");
for( int i = 0; i <= texts.Length; i++) {
Console.WriteLine (texts[i].name); // results in IndexOutOfRangeException
GUI.Button(new Rect(Screen.width - 200, 80+(55*i), 120, 50),texts[i].name.ToString(),PackGui); // results in NullReferenceException
}
}
}
Edit: I tried editing the numberofquestionpacks and replaced it with 3 and 0 but no luck either.
make sure that you have placed your "QuestionPacks" folder in the "Resources" folder.
use for the loop: i < text.Lenght ins$$anonymous$$d of i <= text.Lenght.
to see the Button you have to draw the Button more than one time -> use the OnGUI void.
I already tried all 3 of those. Questionpacks are placed correctly otherwise Debug.log(texts[i].name); wouldnt work.
I swapped it to the OnGUI method as well and still the same errors, except this time they show up constantly.
Exact error is the following: NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.Button (Rect position, System.String text, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUI.cs:384) QuestionPackSelection.OnGUI () (at Assets/QuestionPackSelection.cs:20)
Your GUIStyle is never set to anything, so it will always be null.
yes, it will always be null and should just restore itself to default.
removing it has no effect on the errors so im assu$$anonymous$$g its an issue with the array
What does your code look like now with the GUIStyle removed? Because if you are getting it to print the names in that for loop, it should have no problem displaying them in the buttons.
Also, an unassigned GUIStyle won't just be a default value, it will be null, and you should be getting a compiler warning alerting you to this.
Answer by Wolfram · May 05, 2014 at 06:57 PM
Is your problem resolved? According to your comments, that is unclear.
Concerning your original script:
reassigning "texts" to what Resources.LoadAll returns will simply overwrite your array, so the statement before that ("texts = new TextAsset[numberofquestionpacks];") is useless and can be removed.
Your for-loop goes from i == 0 to i == texts.Length, which is one too many. Always use "i < someArray.Length", instead of "Comment
I guess my issue is finally solved. Interesting. I restarted unity and everything was fine. Null exception error gone.
Thanks.