0

I have two scripts; Localization.cs and LocalizeText.cs. I'm trying to access Localization.cs from LocalizeText.cs. When I access the GetLocalString() function, I receive an error. The error says :

"NullReferenceException: Object reference not set to an instance of an object Localization.GetLocalString (Int32 menuId) (at Assets/Scripts/Localization.cs:162) LocalizeText.Awake () (at Assets/Scripts/LocalizeText.cs:46)"

I'm accessing Localization.cs from LocalizeText.cs using Getcomponent<>. Could you help me please.

Thank you for your time.

Localization.cs Script :

using UnityEngine;

using System.Collections;
using System.Collections.Generic;
using JsonFx.Json;
using UnityEngine.UI;
using System.IO;
using System;

public class Localization : MonoBehaviour {
public TextAsset LanguageFile;
private List<Language> _languages ;

private enum Languages
{
    Turkish = 0,
    English = 1,
    German = 2,
    French = 3,
    Russian = 4,
    Portuguese = 5,
    Spanish = 6
}

private void Awake()
{        
    _languages = new List<Language>(JsonReader.Deserialize<Language[]>(LanguageFile.text));
    PlayerPrefs.SetInt("Language", 0);
    Debug.Log(GetLocalString(6));//it works
}



public string GetLocalString(int menuId)
{
    string value = _languages[PlayerPrefs.GetInt("Language")].menu.MenuStrings[Convert.ToInt32(menuId)];
    return value;
}

public class Language
{
    public int LanguageId;
    public Menu menu;
    public Language()
    {
        menu = new Menu();
    }        
}

public class Menu
{
    public List<string> MenuStrings;

    public Menu()
    {
        MenuStrings = new List<string>();
    }
}

}

LocalizaText.cs Script :

  using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class LocalizeText : MonoBehaviour {

private Text _text;
private Localization _localization;

public enum MenuItems
{
    Start = 0,
    Garage = 1,
    WatchVideo = 2,
    TiltControl = 3,
    GamePadControl = 4,
    Yes = 5,
    Continue = 6,
    CityRoad = 7,
    SnowyRoad = 8,
    Ridgeway = 9,
    MainMenu = 10,
    Exit = 11,
    Speed = 12,
    Acceleration = 13,
    Brake = 14,
    Play = 15,
    Loading = 16,
    LeaderBoard = 17,
    Quests = 18,
    Achivements = 19,
    No = 20,
    AreYouSure = 21

}

public MenuItems MenuItemStrings;

private void Awake()
{
    _text = GetComponent<Text>();
    _localization = GameObject.Find("Localization").GetComponent<Localization>();
    Debug.Log(_localization);
    Debug.Log(_localization.GetLocalString(System.Convert.ToInt32(MenuItemStrings)));
}

}

Output : enter image description here

Halil Cosgun
  • 427
  • 3
  • 10
  • 24
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – maraaaaaaaa Sep 14 '15 at 13:40

2 Answers2

1

private List<Language> _languages; gets filled in the Awake method, and the other script tries to access it also in an Awake method. So what happens when the Script trying to access a language from the list has it's Awake method run first? Before the method to populate the list?

It gets a NullReferenceException.

So try either throwing the accessing block into the Start method, and if that doesn't work, then theres either something wrong with the json parsing, or the key just doesn't exist.

maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37
0

Ill assume what LOLslowSTi said its the problem, you have both methods in Awake. You can change it to Start as he said. Or you can change the script execution order.

Documentation: http://docs.unity3d.com/Manual/class-ScriptExecution.html

Jorge Santos
  • 546
  • 1
  • 7
  • 17