x


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?

more ▼

asked Apr 30 '11 at 10:22 AM

Naim gravatar image

Naim
16 3 3 3

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

Since you're creating this in script anyway, just remove the ": MonoBehaviour" from the class -- make it a regular class, not a component.

more ▼

answered May 01 '11 at 12:49 AM

yoyo gravatar image

yoyo
6.4k 25 39 84

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?

May 01 '11 at 11:01 AM Naim

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.

May 02 '11 at 05:04 AM yoyo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3329
x418
x59
x26
x23

asked: Apr 30 '11 at 10:22 AM

Seen: 1162 times

Last Updated: Apr 30 '11 at 10:22 AM