- Home /
StringTable manager singleton creation
I'm attempting to create a StringTableMgr class that will load and keep track of string tables, but since it has to be globally accessible, I'm looking at this:
http://forum.unity3d.com/threads/35617-TextManager-Localization-Script
to see how to create a script that can be globally accessed without having to have an instance in each game object.
My problem is that when I do it in the same way as the article I get a "You are not allowed to call Internal_CreateGameObject when declaring a variable" error.
public class StringTableMgr : MonoBehaviour { public class StringTable { // blah, unrelated }
static StringTableMgr m_StringTableMgr;
List<StringTable> m_StringTableList;
private static StringTableMgr Instance
{
get
{
if (m_StringTableMgr == null)
{
GameObject notificationObject = new GameObject("StringTableMgr");
m_StringTableMgr = (StringTableMgr) notificationObject.AddComponent(typeof(StringTableMgr));
m_StringTableMgr.m_StringTableList = new List<StringTable>();
}
return m_StringTableMgr;
}
}
public static StringTableMgr GetInstance()
{
return Instance;
}
void Awake()
{
GetInstance();
}
public static bool LoadStringTable(string stringTableName)
{
StringTableMgr stringTableMgr = GetInstance();
foreach(StringTable table in stringTableMgr.m_StringTableList)
{
}
// blah
}
}
I get the error on the line at the top where I create the GameObject. If I leave the creation code, and reference the 'm_StringTableMgr' var directly, I get an null reference exception on the 'foreach' statement in LoadStringTable, which could be either the StringTableMgr var or the 'm_StringTableList'
I don't know what I'm doing wrong, since I'm fairly certain if this code were to run, it would work, since by calling GetInstance() in the Awake() function, I'm basically assuring the creation of the StringTableMgr object and it's data structures.
Can anyone see anything that I've missed, or think of something that I should know but don't?
Answer by yoyo · May 01, 2011 at 12:49 AM
Since you're creating this in script anyway, just remove the ": MonoBehaviour" from the class -- make it a regular class, not a component.
Okay, that's fixed part of it, but I'm loading a text asset using Resources.Load and then using a StringReader to parse the text from it, and it's giving me a "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?
Sounds like a new issue -- maybe ask it as a new question, and include some of your code? Resources.Load should be callable from anywhere.
Your answer
Follow this Question
Related Questions
calculate terms in runtime 3 Answers
Calling a script with name 2 Answers
String split script 1 Answer
Accessing a function through its name stored in a string JS 2 Answers
HealthPack Script Problem... 1 Answer