0

I have some error and I don't know why after adding some localization system to my game turn null reference at the gameplay

after build to android apk its keep showing this error

and this my GameManager script it, I think I have done with an object reference in unity editor but after build to android that the object become null reference

private void Start()
{
    LM = FindObjectOfType<LocalizationManager>();
    audioManager = FindObjectOfType<AudioManager>();
    questionManager = FindObjectOfType<_questionManager>();
    LevelSelect = FindObjectOfType<SelecLevel>();
    Select = FindObjectOfType<_CharacterSelect>();
    sliderChanges = FindObjectOfType<SliderChanges>();

    onlepel = LevelSelect.levelIndex + 1;
    IndexLevel = LevelSelect.levelIndex;
    getPlayerData();

    Timecount = GameObject.FindGameObjectWithTag("TimeCount");

    if (LM.Bahasa)
    {
        category = GameObject.Find("QuestionManagerIndonesia").GetComponent<_questionManager>().category;
    }
    else
    {
        category = GameObject.Find("QuestionManagerEnglish").GetComponent<_questionManager>().category;
    }
    moneyAmount = PlayerPrefs.GetFloat("MoneyAmount");


    if (unansweredQuestion == null || unansweredQuestion.Count == 0)
    {
        thisQuestions = category[IndexLevel].questions;
        unansweredQuestion = new List<Question>(thisQuestions);

        TrueAnswerText.text = "CORRECT";
        FalseAnswerText.text = "WRONG!";

        //unansweredQuestion = new List<Question>(questions);
    }
    if (TrueAnswerText != null)
        TrueAnswerText.text = "CORRECT";
    if (FalseAnswerText != null)
        FalseAnswerText.text = "WRONG!";

    TrueCount = 0;
    if (FactText != null)
        setCurrentQuestion();


}
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 1
    well .. it happens either here `LevelSelect.levelIndex` or here `category[IndexLevel].questions` ... Where and how do you initialize the `category`? Or in `TrueAnswerText.text =` and `FalseAnswerText.text =` - the ones that happen before the `null` check ... – derHugo Sep 23 '19 at 06:58
  • thanks, I found the problem, it's because I have an instance the object and became static so that's `category` can't load 2 references. `static _questionManager instance;` `if (instance != null) { Destroy(gameObject); } else instance = this; DontDestroyOnLoad(gameObject);` –  Sep 23 '19 at 07:07

1 Answers1

1

Put your start block into a try catch

EG.:

try
{
    // A class that has an int field called var
    Test asd = null;
    // whops null reference
    int var = asd.var;
}
catch (NullReferenceException ex)
{
     Console.WriteLine(ex.StackTrace);
}

StackTrace porperty should tell you at which line your nullreference happened.

To enable this feature in release mode check this: Display lines number in Stack Trace for .NET assembly in Release mode

Menyus
  • 6,633
  • 4
  • 16
  • 36
  • 1
    thanks, I found the problem, it's because I have an instance the object and became static so that's `category` can't load 2 references. so I delete that and only put `DontDestroyOnload ` `static _questionManager instance;` `if (instance != null) { Destroy(gameObject); } else instance = this; DontDestroyOnLoad(gameObject);` –  Sep 23 '19 at 07:10