Cleaning Code even more

Below, I created method for setting the button sprite and a ternary to determine the correct question text update. Cleans the code significantly.

public void OnAnswerSelected(int index)
        int correctAnswerIndex = question.GetCorrectAnswerIndex();

        SetButtonImage(correctAnswerIndex, correctAnswerSprite);

        questionText.text = (index == correctAnswerIndex)
            ? "Correct!"
            : "Sorry, the correct answer was\n" + question.GetAnswer(correctAnswerIndex);

    void SetButtonImage(int index, Sprite sprite)
        Image buttonImage = answerButtons[index].GetComponent<Image>();
        buttonImage.sprite = sprite;
1 Like

Privacy & Terms