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 /
This question was closed Jan 20, 2021 at 10:21 PM by Ensive for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Ensive · Jan 20, 2021 at 10:59 PM · scripting problem2d gamelocalizationnon-static

(Localization System) Change Language Problem

Hello!
I made a localization system according to the lesson from Game Dev Guide. However, I ran into the problem of creating a button that changes the language during the game.
I created an event in which the current language is checked and changed, but for the language to change in the game in real time, I need to execute an event from another script, at this point I get an error:

Assets\Scripts\LocalisationSystem.cs(121,9): error CS0120: An object reference is required for the non-static field, method, or property 'TextLocaliserUI.Start()'

 using System;
 using System.IO;
 using System.Linq;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LocalisationSystem : MonoBehaviour
 {
     public enum Language
     {
         English,
         Russian
     }
      
     public static Language language = Language.English;
 
     private static Dictionary<string, string> localisedEN;
     private static Dictionary<string, string> localisedRU;
 
     public static bool isInit;
     
     public static CSVLoader csvLoader;
 
     public static void Init()
     {
         csvLoader = new CSVLoader();
         csvLoader.LoadCSV();
 
         UpdateDictionaries();
 
         isInit = true;
     }
     
     public static void UpdateDictionaries()
     {
         localisedEN = csvLoader.GetDictionaryValues("en");
         localisedRU = csvLoader.GetDictionaryValues("ru");
     }
 
     public static Dictionary<string, string> GetDictionaryForEditor()
     {
         if(!isInit) { Init(); }
         return localisedEN;
     }
 
     public static string GetLocalisedValue(string key) 
     {
         if(!isInit) { Init(); }
 
         string value = key;
 
         switch(language)
         {
             case Language.English:
             localisedEN.TryGetValue(key, out value);
             break;
             case Language.Russian:
             localisedRU.TryGetValue(key, out value);
             break;
         }
 
         return value;
     }
 
     public static void Add(string key, string value)
     {
         if(value.Contains("\""))
         {
             value.Replace('"', '\"');
         }
 
         if(csvLoader == null)
         {
             csvLoader = new CSVLoader();
         }
 
         csvLoader.LoadCSV();
         csvLoader.Add(key, value);
         csvLoader.LoadCSV();
 
         UpdateDictionaries();
     }
 
     public static void Replace(string key, string value)
     {
         if(value.Contains("\""))
         {
             value.Replace('"', '\"');
         }
 
         if(csvLoader == null)
         {
             csvLoader = new CSVLoader();
         }
 
         csvLoader.LoadCSV();
         csvLoader.Edit(key, value);
         csvLoader.LoadCSV();
 
         UpdateDictionaries();
     }
 
     public static void Remove(string key)
     {
         if(csvLoader == null)
         {
             csvLoader = new CSVLoader();
         }
 
         csvLoader.LoadCSV();
         csvLoader.Remove(key);
         csvLoader.LoadCSV();
 
         UpdateDictionaries();
     }
 
     public static void SetLanguage(Language newLanguage)
     {
         language = newLanguage;
         TextLocaliserUI.Start(); // Error
     }
 
     public void ChangeLang()
     {
         if(language == Language.English)
         {
             Debug.Log("Language is set to " + language);
             LocalisationSystem.SetLanguage(LocalisationSystem.Language.Russian);
         }
         else
         {
             Debug.Log("Language is set to " + language);
             LocalisationSystem.SetLanguage(LocalisationSystem.Language.English);
         }
     }
 }

TextLocaliserUI.cs

 using System.IO;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
 [RequireComponent(typeof(TextMeshProUGUI))]
 public class TextLocaliserUI : MonoBehaviour
 {
     TextMeshProUGUI textField;
 
     public LocalisedString localisedString;
 
     public void Start()
     {
         textField = GetComponent<TextMeshProUGUI>();
         textField.text = localisedString.value;
     }
 }


LocalisedString.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public struct LocalisedString
 {
     public string key;
 
     public LocalisedString(string key)
     {
         this.key = key;
     }
 
     public string value
     {
         get
         {
             return LocalisationSystem.GetLocalisedValue(key);
         }
     }
 
     public static implicit operator LocalisedString(string key)
     {
         return new LocalisedString(key);
     }
 }
button.jpg (74.0 kB)
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

233 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiation Disables Objects Scripts and Colliders 1 Answer

Mouse Click Walk to Idle Animations 0 Answers

Get a single number for rotation (2d game) when using Quaternion.FromToRotation 1 Answer

Unity Admob didint show in android device 0 Answers

Cannot create mesh from code,Cannot use Script to Generate 2d mesh 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