(Unity)我的文本UI没有更新

时间:2016-08-19 17:09:45

标签: c# user-interface unity3d text

好的,所以,我尝试做一些类似于quizz的东西,其中必须向Text UI显示一个字符串数组。事情是,在我正确回答之后,问题在检查员中更新,但是它在游戏屏幕上没有。问一个菜鸟的错误,抱歉我是新手。

    public string[] quesitons = new string[] { "2+2 = ?", "1+1 = ?", "3+3 = ?" };
    public string[] answers = new string[] { "4", "2", "6" };
    public int i = 0;

    [SerializeField]
    private InputField _input;

    //Main
    public string currentQ;
    public string currentA;

    public Text questionText;


    public void GetInput(string input)
    {
        if (input == answers[i])
        {
            Debug.Log("Correct");
            i++;
            questionText.text = quesitons[i];
            currentQ = quesitons[i];
            currentA = answers[i];
        }
        else
        {
            Debug.Log("Wrong");
        }
    }

    void Start()
    {
        currentQ = quesitons[i];
        questionText.text = quesitons[i];

        currentA = answers[i];
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您发布的代码应该有效。也许你错过了那里的GetInput函数的文本更新行?无论如何,我在这里发布了一个重构的代码。用下面的代码替换你的GetInput和Start函数,并测试..

public void GetInput(string input) {
    if (input == currentA) {
        Debug.Log("Correct");
        i++;
        LoadQuestionAnswer();
    }
    else {
        Debug.Log("Wrong");
    }
}

void Start() {
    LoadQuestionAnswer();
}

void LoadQuestionAnswer() {
    currentQ = quesitons[i];
    currentA = answers[i];
    questionText.text = currentQ;
}