Cannot tap the button, button error

why when i play my game (quiz master) its cannot be interact??but the timer is stil running
btw sorry for my bad english, im still learning :smiley:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using TMPro;

using UnityEngine.UI;

public class Quiz : MonoBehaviour

{

[Header("Question")]

[SerializeField]TextMeshProUGUI questionText;

[SerializeField]QuestionSO question;

[Header("Answer")]

[SerializeField] GameObject[] answerButtons;

int correctAnswerIndex;

[Header("Button Color")]

[SerializeField] Sprite defaultAnswerSprite;

[SerializeField] Sprite correctAnswerSprite;

[Header("Timer")]

[SerializeField] Image timerImage;

Timer timer;

bool hasAnsweredEarly = false;

void Start()

{

  timer = FindObjectOfType<Timer>();

  GetNextQuestion();

   

}

void Update()

{

    timerImage.fillAmount = timer.fillFraction;  

    if(timer.loadNextQuestion)

    {

          hasAnsweredEarly = false;

         GetNextQuestion();

         timer.loadNextQuestion = false;

    }

    else if(!hasAnsweredEarly && !timer.isAnsweringQuestion)

    {

        DisplayAnswer(-1);

        SetButtonState(false);

    }

}

public void OnAnswerSelected(int index)

{

    DisplayAnswer(index);  

    SetButtonState(false);

    timer.cancelTimer();

    hasAnsweredEarly = true;

   

}

void DisplayAnswer(int index)

{

      Image buttonImage;

    if (index == question.getCorrectAnswerIndex())

    {

        questionText.text = "BENARRR!!";

        buttonImage = answerButtons[index].GetComponent<Image>();

        buttonImage.sprite = correctAnswerSprite;

    }

    else

    {

        correctAnswerIndex = question.getCorrectAnswerIndex();

        string correctAnswer = question.getanswer(correctAnswerIndex);

        questionText.text = "Yahhh, tapi jawaban yang benarnya adalah :\n" + "\n" + correctAnswer;

        buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();

        buttonImage.sprite = correctAnswerSprite;

    }

}

void GetNextQuestion()

{

    SetButtonState(true);

    SetDefaultButtonSprites();

    DisplayQuestion();

}



void DisplayQuestion()  

{

     questionText.text = question.getQuestion();



    for (int i = 0; i < answerButtons.Length; i++)

    {

        TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();buttonText.text = question.getanswer(i);

    }

}

 void SetButtonState(bool state)

    {

        for (int i = 0; i< answerButtons.Length; i ++)

        {

            Button button = answerButtons[i].GetComponent<Button>();

            button.interactable = state;

        }

   

    }

void SetDefaultButtonSprites()

    {

        for (int i = 0; i < answerButtons.Length; i++)

        {

            Image buttonImage = answerButtons[i].GetComponent<Image>();

            buttonImage.sprite = defaultAnswerSprite;

        }

    }

}

Hi Juniko,

In the Button Component in the Inspector, is the OnClick() field connected with the OnAnswerSelected method in your Quiz object?

Have you already tried to add Debug.Logs to your code to see what is going on during runtime?


See also:


it was connected with OnAnswerSelected

Then add a Debug.Log to OnAnswerSelected to see if the method gets called when you click the button.

If the message does not appear, check the value of button.interactable. In the SetButtonState method, the value gets set, so maybe your code makes the button not interactable. At some point, button.interactable has to be set to true again. Try to figure out if that happens while your game is running.

im so sorryy but when i add Debug.Log again i getting error :(((

Debug.Log requires a string inside the parentheses. That’s what the error message tries to tell you.

its still not working :frowning:

What did you try? Please share your current code as formatted text.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using TMPro;

using UnityEngine.UI;

public class Quiz : MonoBehaviour

{

[Header("Question")]

[SerializeField]TextMeshProUGUI questionText;

[SerializeField]QuestionSO question;

[Header("Answer")]

[SerializeField] GameObject[] answerButtons;

int correctAnswerIndex;

[Header("Button Color")]

[SerializeField] Sprite defaultAnswerSprite;

[SerializeField] Sprite correctAnswerSprite;

[Header("Timer")]

[SerializeField] Image timerImage;

Timer timer;

bool hasAnsweredEarly = false;

void Start()

{

  hasAnsweredEarly = false;  

  timer = FindObjectOfType<Timer>();

  SetButtonState(true);

  GetNextQuestion();    

}

void Update()

{

    timerImage.fillAmount = timer.fillFraction;  

    if(timer.loadNextQuestion)

    {

        hasAnsweredEarly = false;

        GetNextQuestion();

        timer.loadNextQuestion = false;

    }

    else if(!hasAnsweredEarly && !timer.isAnsweringQuestion && timer.timerValue <= 0)

    {

        DisplayAnswer(-1);

        SetButtonState(false);

    }

}

public void OnAnswerSelected(int index)

{

    hasAnsweredEarly = false;  

    DisplayAnswer(index);

    SetButtonState(false);

    timer.cancelTimer();

    Debug.Log("Button clicked, index: " + index);

}

void DisplayAnswer(int index)

{

      Image buttonImage;

    if (index == question.getCorrectAnswerIndex())

    {

        questionText.text = "BENARRR!!";

        buttonImage = answerButtons[index].GetComponent<Image>();

        buttonImage.sprite = correctAnswerSprite;

    }

    else

    {

        correctAnswerIndex = question.getCorrectAnswerIndex();

        string correctAnswer = question.getanswer(correctAnswerIndex);

        questionText.text = "Yahhh, tapi jawaban yang benarnya adalah :\n" + "\n" + correctAnswer;

        buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();

        buttonImage.sprite = correctAnswerSprite;

    }

}

void GetNextQuestion()

{

    SetButtonState(false);

    SetDefaultButtonSprites();

    DisplayQuestion();

}



void DisplayQuestion()  

{

     questionText.text = question.getQuestion();



    for (int i = 0; i < answerButtons.Length; i++)

    {

        TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();buttonText.text = question.getanswer(i);

    }  

}

 void SetButtonState(bool state)

    {

         Debug.Log("SetButtonState: " + state);

         for (int i = 0; i< answerButtons.Length; i++)

        {

            Button button = answerButtons[i].GetComponent<Button>();

            button.interactable = state;

            Debug.Log("Button " + i + " interactable: " + button.interactable);

        }

   

    }

void SetDefaultButtonSprites()

    {

        for (int i = 0; i < answerButtons.Length; i++)

        {

            Image buttonImage = answerButtons[i].GetComponent<Image>();

            buttonImage.sprite = defaultAnswerSprite;

        }

    }

}

sorry for a lot edit…i just follow what chat gpt say :’

but its still dont fix that bug

Which bug?

This was the last error you posted, and from what I understood, you still have it.
image

Unfortunately, I cannot spot any problem in your code. Is there a new or another error message in your Console?

for the error that has been corrected

bug i mean is when I started his game, he immediately corrected my answer. and the buttons don’t work at all. For the onclick is correct, it is still connected to the button

thank u very much it just fixed :slight_smile:

You’re welcome. :slight_smile:

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

Privacy & Terms