- Home /
Why are my static member variables null inside other classes/scripts?
In my application, there is a need for all of my strings to be multilingual. My first plan was to import key-value pairs as text assets and parse these per individual scene. Being rather new to Unity, however, I couldn't figure out how to access this data from other classes. I was told that this could be easily achieved using static member variables and functions. I figured that, considering I am not very familiar with Unity yet, as opposed to the background I came from where the contents of static variables are lost across scripts, things might be handled differently here.
So as to not make my script too complicated on the get-go, I have currently come to (and have removed most of the irrelevant code):
using UnityEngine;
using System.Collections.Generic;
public class LanguageManager : MonoBehaviour
{
public static string SelectedLanguage { get; set; }
public static Dictionary<string, Dictionary<string, string>> Strings { get; set; }
void Start()
{
LanguageManager.SelectedLanguage = "en";
LanguageManager.Strings = new Dictionary<string, Dictionary<string, string>>();
LanguageManager.Strings.Add("en", new Dictionary<string, string>() {
{ "test_string_01", "This is a test sentence." },
{ "test_string_02", "This is another test sentence." },
{ "test_string_03", "Formatting? {0}!" }
});
LanguageManager.Strings.Add("nl", new Dictionary<string, string>() {
{ "test_string_01", "Dit is een testzin." },
{ "test_string_02", "Dit is een andere testzin." },
{ "test_string_03", "Formatteren? {0}!" }
});
}
}
In this script, the values are fine and everything works as I want it to. In any other script, referring to any member variable returns null. I know there are alternatives to my issue, but I'm wondering if static variables being lost across scripts still holds true here as expected, or if I'm just doing something wrong.
I apologise if it still turns out I didn't look hard enough or am missing something incredibly obvious.
Thanks in advance!
Answer by Jessespike · Aug 18, 2014 at 10:05 PM
How are you accessing the strings in the other scripts? In their Start functions? Try changing your LanguageManager Start() to an Awake(). Everything works OK for me. Also make sure you place LanguageManager onto a GameObject in the scene, because it's a MonoBehaviour.
More info about Script Execution Order: http://docs.unity3d.com/Manual/ExecutionOrder.html
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LanguageManagerTest : MonoBehaviour {
// Use this for initialization
void Start () {
Dictionary<string, string> myDictionaries;
string myText = "";
LanguageManager.Strings.TryGetValue( "en", out myDictionaries );
myDictionaries.TryGetValue( "test_string_01", out myText );
Debug.Log(myText);
}
}
Changing Start to Awake within my language manager has resolved the issue. That's also an excellent piece of documentation I most definitely should have read earlier. Thanks a lot!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
UnityEngine Assert IsNotNull not working on empty inspector field 1 Answer
Using static variables (or similar) to affect instances of the same object separately. 2 Answers
FindWithTag is returing true, and while GetComponent is returning null...seemly random 1 Answer