Dont destroy object when changing scene
I'm using some scripts I found to change the language in my App. I've got a scene where I initially set the language then it changes it throughout the rest of the App.
However, when I return to the language selection menu, it defaults back to the start language (English) and I'm unable to change the language.
The Console shows the following error: MissingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
I can't work out specifically which part of the script is doing this; I have 2 main scripts:
using System.Runtime.InteropServices;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public static class TextLocalization
{
public static event Localize LocalizationChanged;
private static readonly Dictionary<string, string[]> dictionary = new Dictionary<string, string[]>();
private static TextLocalizationFiles localizationAsset;
private static int nbLocaFiles;
private static int currentLanguageIndex = -1;
public static string[] AvailableLanguages { get; private set; }
public static string CurrentLanguage { get { return AvailableLanguages[currentLanguageIndex]; } }
public static List<string> Keys { get { return new List<string>(dictionary.Keys); }}
public static void Init()
{
localizationAsset = Resources.Load<TextLocalizationFiles>("Localization");
if (nbLocaFiles != localizationAsset.files.Length || dictionary.Count == 0)
{
dictionary.Clear();
foreach (TextAsset asset in localizationAsset.files)
Load(asset);
nbLocaFiles = localizationAsset.files.Length;
}
}
public static bool Load(TextAsset txt)
{
if (txt != null && LoadCSV(txt))
return true;
return false;
}
private static bool LoadCSV(TextAsset asset)
{
CSVReader reader = new CSVReader(asset);
// The first line should contain "KEY", followed by languages.
List<string> temp = reader.ReadCSV();
// There must be at least two columns in a valid CSV file
if (temp.Count < 2) return false;
// Ensure that the first value is what we expect
if (!string.Equals(temp[0], "KEY"))
{
Debug.LogWarning("Invalid localization CSV file. The first value is expected to be 'KEY', followed by language columns.\n" +
"Instead found '" + temp[0] + "'", asset);
return false;
}
if (AvailableLanguages == null)
{
AvailableLanguages = new string[temp.Count - 1];
for (int i = 1; i < temp.Count; ++i)
AvailableLanguages[i - 1] = temp[i];
}
else
{
if (AvailableLanguages.Length != temp.Count - 1)
{
Debug.LogError(
string.Format("Localization file {0} does not have the correct amount of languages, expected {1} but got {2}",
asset.name, AvailableLanguages.Length, temp.Count - 1));
return false;
}
}
// Read the entire CSV file into memory
temp = reader.ReadCSV();
while (temp != null)
{
string[] values = new string[temp.Count - 1];
for (int i = 0; i < values.Length; ++i) values[i] = temp[i + 1];
try
{
dictionary.Add(temp[0], values);
}
catch (System.Exception)
{
Debug.LogError(string.Format("Unable to add '{0}' to the Localization dictionary. Found the double in {1}", temp[0], asset.name));
}
temp = reader.ReadCSV();
}
return true;
}
/// <summary>
/// Select the specified language from the previously loaded CSV file.
/// </summary>
public static bool SelectLanguage(string alanguage)
{
currentLanguageIndex = -1;
for(int i = 0; i < AvailableLanguages.Length; ++i)
if (AvailableLanguages[i] == alanguage)
{
currentLanguageIndex = i;
break;
}
if (Application.isPlaying)
{
if(LocalizationChanged != null)
LocalizationChanged();
}
return currentLanguageIndex >= 0;
}
public static string Get(string key)
{
string[] vals;
if (currentLanguageIndex != -1 && dictionary.TryGetValue(key, out vals))
{
if (currentLanguageIndex < vals.Length)
return vals[currentLanguageIndex];
}
#if UNITY_EDITOR
Debug.LogWarning("Localization key not found: '" + key + "'");
#endif
return key;
}
public static string[] GetAll(string key)
{
string[] vals;
if (dictionary.TryGetValue(key, out vals))
return vals;
#if UNITY_EDITOR
Debug.LogWarning("Localization key not found: '" + key + "'");
#endif
return null;
}
public static bool Exists(string key)
{
return dictionary.ContainsKey(key);
}
}
public delegate void Localize();
And this script:
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class TextLocalize : MonoBehaviour
{
public string key;
private Text label;
public string value
{
set
{
if (!string.IsNullOrEmpty(value))
{
label.text = value;
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(label);
#endif
}
}
}
private void Awake()
{
TextLocalization.LocalizationChanged += OnLocalize;
label = GetComponent<Text>();
}
private void Destroy()
{
TextLocalization.LocalizationChanged -= OnLocalize;
}
private void OnEnable()
{
#if UNITY_EDITOR
if (!Application.isPlaying) return;
#endif
OnLocalize();
}
private void Start()
{
#if UNITY_EDITOR
if (!Application.isPlaying) return;
#endif
OnLocalize();
}
public void OnLocalize()
{
if (string.IsNullOrEmpty(key))
key = label.text;
if (!string.IsNullOrEmpty(key)) value = TextLocalization.Get(key);
}
}
I thought it would just be a case of deleting the private void Destroy(){ function but this does nothing.
Which led me to believe it is something to do with the public delegate void Localize(); function.
Can anyone point me in the right direction please?
In What line throw the error?
And your OnEnable and Start do the same, you can remove Start.
Thanks for replying. I have several error references: $$anonymous$$issingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. TextLocalize.set_value (System.String value) (at Assets/LocaDemo/Assets/Localize/TextLocalize.cs:18) TextLocalize.OnLocalize () (at Assets/LocaDemo/Assets/Localize/TextLocalize.cs:48) TextLocalization.SelectLanguage (System.String alanguage) (at Assets/LocaDemo/Assets/Localize/TextLocalization.cs:112) GameController.Awake () (at Assets/LocaDemo/Assets/GameController.cs:7)
GameController script: using UnityEngine; public class GameController : $$anonymous$$onoBehaviour { private void Awake () { TextLocalization.Init(); TextLocalization.SelectLanguage("English"); } public void SelectLanguage(string aName) { TextLocalization.SelectLanguage(aName); } }
Answer by Tyche10 · May 11, 2016 at 10:23 AM
I don't see you calling the Destroy function anywhere so it won't get called, and so you don't unsubscribe your listener to the event, which can throw a null reference. I think you probably meant to use the OnDestroy() function: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html
Thanks for the reply.
I don't want to destroy the Text object but when I return to the language selection screen (containing the GameController script) it comes up with the error: $$anonymous$$issingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
One of the scripts must be destroying the Text objects and preventing me from changing the language again.
Any ideas? (See the comment on the question to see the full error message)
I gather this problem occurs when you are changing scenes? Have you tried using the DontDestroyOnLoad function? http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Even if you don't want to destroy, you should always coorectly unsubscribe an event listener from the event publisher before the listener is destroyed
private void OnDisable()
{
TextLocalization.LocalizationChanged -= OnLocalize;
}
Thank you!
Putting in the OnDisable script now means I can go back to be Select Language screen to select a different language without it causing errors.
The only thing that isn't quite right is that it reverts back to English because of the GameControllers Awake function:
private void Awake () { TextLocalization.Init(); TextLocalization.SelectLanguage("English"); }
Your answer
Follow this Question
Related Questions
How can I delete a line of UI Text? 0 Answers
Text in UnityEngine.UI doesn't work 0 Answers
GameObject destroyed when changing scene 1 Answer
Array cannot read Greek characters 1 Answer
Text in TextMeshPro looks weird 0 Answers