简体   繁体   中英

(Unity) My Text UI not updating

Ok, so, i try to make something like a quizz, where an array of strings has to be shown to the Text UI. Thing is, the question updates in the inspector after i answer it correctly, however it doensn't on the game screen. Prob a noob mistake, sorry i'm newbie.

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

在此处输入图片说明

The code you have posted should work. Maybe you've missed the text updation line from the GetInput function there? Anyway I've posted a refactored code here. Replace your GetInput and Start functions with below code, and test..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM