Load issues

Hi!,

I’m trying to load a XML file and so far I got it, but I get the infamous “text node can’t appear in this state”. Something to do with the BOM encoding.

Anyways, I tried this:

//--------------------------------------------------------------------------

import System.Xml;

import System.IO;

//--------------------------------------------------------------------------

function GetTextWithoutBOM(textAsset : TextAsset)
{

   var memoryStream : MemoryStream = new MemoryStream(textAsset.bytes);
   var streamReader : StreamReader = new StreamReader(memoryStream, true);

   var result : String = streamReader.ReadToEnd();

   streamReader.Close();
   memoryStream.Close();

   return result;
}

//--------------------------------------------------------------------------

function loadLevel(archXML : String){

var xmlDoc : XmlDocument = new XmlDocument();

var ta : TextAsset = Resources.Load(“/Assets/Materials/XML/Levels/10/test.xml”);

Debug.Log(ta); // — Returns NULL always :frowning:

var noBom: String = GetTextWithoutBOM(ta);

xmlDoc.LoadXml(noBom);

...

//--------------------------------------------------------------------------

But now I geting an “Object reference not set to an instance of an object” error, and I realize with the “Debug.Log” line that “Resources.Load” never load the file (it’s null always).

But, I know that the path to the file is Ok, because, when I use just this:

xmlDoc.LoadXml(“test.xml”);

  • or -
    xmlDoc.LoadXml(“/Assets/Materials/XML/Levels/10/test.xml”);

It works fine but I get the “Text node canot appear in this state” error in the first character.

What I’m doing wrong??
I’m stuck.

Thanks

var ta : TextAsset = Resources.Load(“/Assets/Materials/XML/Levels/10/test.xml”);Z
Debug.Log(ta); // — Returns NULL always :frowning:

If your text asset is null, I’m not sure why you would expect anything after that to work.

According to Unity’s script reference, Resources.Load() takes an asset name only, as opposed to a complete path. If you want to load a TextAsset named “Foo.txt”, you need to place it in a folder named “Resources” (under any path in your assets folder) and make a call like this:

var foo : TextAsset = Resources.Load("Foo");

There’s a little bit more explanation here. If you’re used to specifying paths directly, this can seem like a bit of a hurdle, but setting this up carefully can make it much easier to port your game to other platforms, if that’s something you have in mind.

From there, Unity might expect that a text asset’s file extension is “txt”, but you’re welcome to experiment with that on your own.

I see, doh!, I checked the script reference but didn’t realize that the folder should be named Resources, I just thought it was an example.

Anyways, I want to place a huge number of text files there, so I would like to have my folders a bit more organized, but it doesn’t seem possible unless I use “LoadAssetAtPath”, that doesn’t work in standalone or web players.

So this raises another question: is it possible to load textAssets with “Resources.Load” that is inside a subfolder that is located within the “Resources” folder?
Example:
text file is in: Assets/Resources/Levels/test.txt
Is there a way to load that file other than using “LoadAssetAtPath”?. I tried with Resources.Load and it didn’t work.

Thanks