【问题标题】:(Unity) My Text UI not updating(Unity) 我的文本 UI 没有更新
【发布时间】:2016-12-26 22:25:28
【问题描述】:

好的,所以,我尝试做一些类似测验的事情,其中​​必须向文本 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];
    }
}

【问题讨论】:

    标签: c# user-interface unity3d text


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      相关资源
      最近更新 更多