- Home /
Using Resources.Load from a non script component class
I have a normal non-script component class (ie. does not inherit from MonoBehaviour, no Awake, Start funcs, etc) and I'm using it to parse and load text file assets, but I keep getting an error: "You are not allowed to call get_text when declaring a variable". Is there some problem using Resources.Load from outside of a Unity script?
{ StringReader reader = null;
TextAsset textAsset = (TextAsset)Resources.Load(stringTableName, typeof(TextAsset));
// blah, error checking
// read the strings from file and insert into the hash table
reader = new StringReader(textAsset.text);
The error happens on the line where I create the StringReader to parse the file. I mean, this is simply a normal class, so it can only be called from script, right? And by the time that a script component is able to create/call functions from this class, unity should be all set up and ready to go, so I'm not sure what the problem is.
Answer by Mike 3 · May 04, 2011 at 03:39 AM
No, you can call it from (almost) anywhere, the issue is that you're assigning when you're declaring it as a member field
What you need to do is assign to it in the constructor probably:
public class YourClass { TextAsset textAsset;
public YourClass()
{
textAsset = (TextAsset)Resources.Load(stringTableName, typeof(TextAsset));
}
}
This may not fix it if the class is being created in a similar way though (i.e. at someplace other than inside an event function called from unity), so at that point you may just need to put the assign into whichever functions use it, after checking if it's null, e.g.
public void Test()
{
if (textAsset == null)
{
textAsset = (TextAsset)Resources.Load(stringTableName, typeof(TextAsset));
}
}
Why is this exactly? I'm writing a StringTable$$anonymous$$gr class, and this is happening inside the function to parse and load a text file as a string table. The function that I'm calling it from is a static function, since the StringTable$$anonymous$$gr is going to be accessed from everywhere in code and it seemed best to implement it as a singleton.
The point is, is that since it is getting called from other unity script code, correct me if I'm wrong, but it is always going to be called somewhere down the call stack from a unity event. So I don't understand as to why this wouldn't work.
Not always, no. Imagine something like $$anonymous$$yClass test = $$anonymous$$yClass.Instance; as a member variable to a class. That won't be called by a unity event, but will be called at some other point ins$$anonymous$$d, which can often lead to screwups
I'm a beginner with Unity scripting, but my impression was that each script has (at least) a Start function and an Update function (and others). Are these unity events? I mean, if my class is only accessed from these (and similar functions), unity has to call these functions, so does that make them unity events?
Or, if my class is called from a constructor of an object that is called from a unity event, does this qualify, or does unity just call it an error because it's not a sure thing?
those are what i meant by unity events, yeah. generally you're ok if you create instances from inside of them, though still probably best to stay clear of assigning objects to member fields inside the created objects
Well then, what I want to do may be impossible in Unity then. But then how does this (http://forum.unity3d.com/threads/35617-Text$$anonymous$$anager-Localization-Script) work? I'm doing the same as him except that my class isn't a Unity script component (doesn't inherit from $$anonymous$$onoBehavior). Is it because he creates and adds his object to a GameObject?
Answer by Naim · May 10, 2011 at 03:24 AM
This is a commercial project, so I don't know if I can post any of the specific code, but I've found that even when trying a cut down test class, with the exact same form, that the error doesn't occur.
The test class is:
public class TempClass { static TempClass m_TempClass; List<string> m_StringList; StringReader reader;
private static TempClass Instance { get { if (m_TempClass == null) { m_TempClass = new TempClass(); } return m_TempClass; } }
public static TempClass GetInstance() { return Instance; }
private TempClass() { m_StringList = new List<string>(); }
public static string LoadString(string fileName) { TempClass tempClass = GetInstance();
string temp = ParseFile(fileName);
return temp;
}
static string ParseFile(string fileName) { string temp;
TextAsset textAsset = (TextAsset)Resources.Load(fileName, typeof(TextAsset));
Instance.reader = new StringReader(textAsset.text);
temp = Instance.reader.ReadLine();
for (;
temp != null;
temp = Instance.reader.ReadLine())
{
Instance.m_StringList.Add(temp);
}
return temp;
} }
I don't know how much help it will be to see the code that works, but I'm hoping it sparks something. I've found that in the actual code, the error occurs during the usage of 'textAsset.text' If I replace that with a string literal, then the script compile error doesn't occur.