Resource.Load is not loading assets when string variables are passed as arguments.

Hello all,

I am running into an issue with the following piece of code:

function New(cn : String)
{
	cardName = cn;
	var cardImage : Material = Resources.Load(cardName, typeof(Material)) as Material;
	gameObject.renderer.material = cardImage;
}

The code that I am using to call this is:

New(cardList[1].ToString());

where cardList is an System.Array;

When i run my code and this executes my asset does not load on the gameObject, however if i use a hardcoded string like “Joker” it will load the asset fine. I’m stuck on how to resolve this problem, any ideas?

Thanks in advance.

Debug.Log the cardname - it will work using a string, so the string must be wrong somehow. How do you populate the cardlist? Any chance there's whitespace in there (new line etc)

I did a Debug.Log(cardname) and the results shows: Joker UnityEngine.Debug:Log(Object) I'm populating the cardList by parsing a text file that has the names of the cards private var cardListFile = "CardList.txt"; var sr = new StreamReader(Application.dataPath + "/" + cardListFile); var fileContents = sr.ReadToEnd(); sr.Close(); cardList = fileContents.Split("\n"[0]); Hope this helps.

1 Answer

1

@whydoidoit, Thanks you were right about the new line, it was in there. I grabbed the length of each string and they were larger than expected.

I changed how I was parsing my file and it seemed to do the trick.

The new code:

    var line = "";
    while (true)
    {
    	line = sr.ReadLine();
    	if (line == null) { break; }
    	cardList.Push(line);
    }
    sr.Close();