Hello. I’m confused about what I’m missing? the answer button doesn’t work whether it clicks on the right or wrong answer. Can anyone help me? here’s a screenshot of my unity and code.
Hi,
Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.
You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.
And what does “not work” mean? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?
Hope this helps
See also;
- Forum User Guides : How to apply code formatting within your post
Ah sorry, I forgot to share the codes in the proper format.
I’ve used Debug.Logs to track my error when the answer button is clicked to see if it works when I select the correct answer or not. But there are no Logs that I have made in the console, so I am confused about what error I made so that there is no response when I click the answer button.
My codes:
// Quiz.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class Quiz : MonoBehaviour
{
[SerializeField] TextMeshProUGUI questionText;
[SerializeField] QuestionSO question;
[SerializeField] GameObject[] answerButton;
[SerializeField] int correctAnswerIndex;
[SerializeField] Sprite defaultAnswerSprite;
[SerializeField] Sprite correctAnswerSprite;
void Start()
{
questionText.text = question.GetQuestion();
for (int i = 0; i < answerButton.Length; i++)
{
TextMeshProUGUI buttonText = answerButton[i].GetComponentInChildren<TextMeshProUGUI>();
buttonText.text = question.GetAnswer(i);
}
}
public void OnAnswerSelected(int index)
{
Image buttonImage;
if (index == question.GetCorrectAnswerIndex())
{
questionText.text = "Correct!";
buttonImage = answerButton[index].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
Debug.Log("Correct!");
}
else
{
correctAnswerIndex = question.GetCorrectAnswerIndex();
string correctAnswer = question.GetAnswer(correctAnswerIndex);
questionText.text = "Sorry, the correct answer was;\n" + correctAnswer;
buttonImage = answerButton[correctAnswerIndex].GetComponent<Image>();
buttonImage.sprite = correctAnswerSprite;
Debug.Log("Incorrect!");
}
}
}
// QuestionSO.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "Quiz Question", fileName = "New Question")]
public class QuestionSO : ScriptableObject
{
[TextArea(2, 6)]
[SerializeField] string question = "Enter new question text here";
[SerializeField] string[] answers = new string[4];
[SerializeField] int correctAnswerIndex;
public string GetQuestion()
{
return question;
}
public string GetAnswer(int index)
{
return answers[index];
}
public int GetCorrectAnswerIndex()
{
return correctAnswerIndex;
}
}
Hi there!
I don’t see any issues with your code or hierarchy settings that would cause the problem you are describing, the only thing I can think of is that the buttons are being blocked by one of your background images.
Try deactivating or removing the Graphic Raycaster
component from your Canvases that do not have interactable components (buttons) and see if that solves your problem. If it does solve it here’s why:
https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-GraphicRaycaster.html
I had the same problem, for me it was because the PreVisualizationCanvas was in front of the Quiz canvas so blocking it, if you click on the PreVisualizationCanvas and in the inspector untick the checkbox that could solve the issue.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.