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.

" +
“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*;*
  •  }*
    
  •  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 = 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;*
* }*

* ///

*
* /// Select the specified language from the previously loaded CSV file.*
* ///
*

* public static bool SelectLanguage(string alanguage)*
* {*
* currentLanguageIndex = -1;*
* for(int i = 0; i < AvailableLanguages.Length; ++i)*
_ if (AvailableLanguages == 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();*
* }*

* 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?

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