首页 > 解决方案 > 构建到 android 后对象引用未设置为对象的实例

问题描述

我有一些错误,我不知道为什么在我的游戏中添加了一些本地化系统后,在游戏中转为空引用

构建到 android apk 后,它一直显示此错误

这是我的 GameManager 脚本,我想我已经在统一编辑器中完成了对象引用,但是在构建到 android 之后,该对象变为空引用

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();


}

标签: c#unity3d

解决方案


将你的起始块放入 try catch

例如。:

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);
}

StackTraceporperty 应该告诉你你的空引用发生在哪一行。

要在发布模式下启用此功能,请检查:Display lines number in Stack Trace for .NET assembly in Release mode


推荐阅读