OnAnswerSelected method isn't called

Hello GameDev.tv community,

I’m working on the Unity Quiz Master and facing an issue where my OnAnswerSelected method is not being called upon button click. Below is the code snippet from my Quiz.cs script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro; // TextMeshProUGUI
using UnityEngine.UI; // Image

public class Quiz : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI questionText; // TextMeshProUGUI
    [SerializeField] QuestionSO question; // QuestionSO is a ScriptableObject
    [SerializeField] GameObject[] answerButtons;
    int correctAnswerIndex;
    [SerializeField] Sprite defaultAnswerSprite;
    [SerializeField] Sprite correctAnswerSprite;

    void Start()
    {
        questionText.text = question.GetQuestion();

        for(int i = 0; i < answerButtons.Length; i++)
        {
            TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
            buttonText.text = question.GetAnswer(i);
        }

    }

    public void OnAnswerSelected(int index)
    {
         Debug.Log("OnAnswerSelected method has been called, the selected answer index is: " + correctAnswerIndex);
         
        if(index == question.GetCorrectAnswerIndex())
        {
            questionText.text = "Correct!";
            Image buttonImage = answerButtons[index].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite;
        }
        else
        {
            questionText.text = "Incorrect!";        
        }
    }

}

I’ve set up the UI buttons in the Unity editor and assigned the OnAnswerSelected method to the OnClick event of these buttons. However, when I click on any answer button, nothing happens, and the debug log message doesn’t appear in the console.

Here are the steps I’ve taken:

  • Ensured the Quiz script is attached to the relevant GameObject.
  • Checked that the OnClick event for each button is set to Quiz -> OnAnswerSelected (int) .

I’m unsure why the method is not being triggered. Any insights or suggestions would be greatly appreciated!

Attached are screenshots from my Unity setup:


Thank you!

At a quick glance everything looks fine except I’m not sure where your BackgroundCanvas is. It may be on top of the buttons, meaning the click is blocked by it. What you can do is select the EventSystem object in the hierarchy at runtime and check the bottom of the inspector when you move the mouse over the buttons. It will show what the mouse is currently hovering over. Make sure it is, in fact, the button

Hi bixarrio,

I followed your instruction and you are right! The sort order of my ‘PrevisualizationCanvas’ object is 100 and once I changed that to 0 everything’s fine now. Thank you so much!!

One minor add on questions tho, any idea how to make the sprite for correct answer more… fitting?

The sprites are the same, so make sure the settings on the images (check that it’s set to ‘sliced’) are the same

You are right, I forgot to slice the image. Thank you so much, everything’s working perfectly fine now. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms