TextAsset Giving me NullReference Exception

Ok, this is driving me bonkers. I have searched the forums, but can't find anything that addresses my issue, so here goes.

I'm trying to import a simple text file that describes the height of objects that I want to construct. I had this working completely using monoscript, but since moving to c# I have not been able to import this file, though I have tried implementing several solutions after my own did not work.

The text file holds strings of numbers that I use to determine the scale of Primitives which I am instantiating. My instantiation code works, I just can't load this text file and build using the data it gives me. I have tried placing the asset in various locations, to no avail.

Please let me know what I'm doing wrong. I'm new to c#, so be gentle ;)

using UnityEngine;
using System;
using System.Collections;
using System.IO;

public class LoadLevel : MonoBehaviour {

    //the level as described in a text file   
    public static string[] dataLines;   
    public static int k;

    public static string[] GetLines () {
        ArrayList lines = new ArrayList();
        string line;
        TextAsset textFile = (TextAsset)Resources.Load("text", typeof(TextAsset));
        System.IO.StringReader textStream = new System.IO.StringReader(textFile.text);

        bool match = false;
        while((line = textStream.ReadLine()) != null) {

        if (lines.Count > 0) {
            return (string[])lines.ToArray(typeof(string));
        return new string[0];

    public static void Start(){
        Debug.Log("LoadLevel Start Got Called!");
        dataLines = GetLines();

... ... etc

The error I'm getting is

NullReferenceException: Object reference not set to an instance of an object LoadLevel.GetLines () (at Assets\Standard Assets\Scripts\LoadLevel.cs:20) LoadLevel.Start () (at Assets\Standard Assets\Scripts\LoadLevel.cs:37) Manager.Awake () (at Assets\Standard Assets\Scripts\Manager.cs:30)

I'm hoping this is something simple that I'm just not seeing or that my head is not wrapping properly around.

Any and all help welcome.



Interesting issue here...

The problem is essentially resolved. Walked away, grabbed a coffee, sat back down and realized I had named the asset "text" instead of "test" in the Resource.Load call. So I changed it to the proper name.


When I pressed play in Unity, I got the same error message. Why was that? I decided to quit and restart. I pressed play again: error gone.

Why would the interpreter tell me I had an error that I had resolved?

Side issue, but I hope someone can track that down... it makes debugging a bit of an issue if I have to restart Unity to make sure I've fixed something Unity has told me is broken.

try to create a new folder and name it Resources and put your xml file in it then try again.