Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wicketd · Aug 18, 2014 at 08:19 PM · c#nullglobalstatic variables

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 }




Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wicketd · Aug 18, 2014 at 10:22 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Sending message to one object 1 Answer

Check if Array.GetValue(i) is null 1 Answer

Unity c# Object reference not set to an instance of an Object ( Shooting Script ) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges