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*.name); // results in IndexOutOfRangeException*

_ GUI.Button(new Rect(Screen.width - 200, 80+(55i), 120, 50),,PackGui); // results in NullReferenceException*_
* }*
* }*
Edit: I tried editing the numberofquestionpacks and replaced it with 3 and 0 but no luck either.

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 “<=”.

Your script in your comment should work - if it doesn’t, please explain what exactly happens instead.