Issues

this isn’t working for me
public void OnAnswerSelected(int index)

{

    if(index == question.GetcorrectAnswerIndex())

    {
       
        questionText.text = "Correct!";

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

        buttonImage.sprite = correctAnswerSprite;

    }

}

this is the error message i get, ‘QuestionSO’ does not contain a definition for ‘GetcorrectAnswer’ and no accessible extension method ‘GetcorrectAnswer’ accepting a first argument of type ‘QuestionSO’ could be found (are you missing a using directive or an assembly reference?)

It just means you are trying to call a method that doesn’t exist. It looks like you may have a typo. Try GetCorrectAnswerIndex() - with an uppercase ‘C’)

tried that. still not working

OK, well it’s hard to help if you are not giving me anything. That error means you are trying to call a method that doesn’t exist. According to it GetcorrectAnswer does not exist on QuestionSO. Check QuestionSO and see what the method is you are trying to call and use that instead of GetcorrectAnswer.

I checked the course repo and it should be GetCorrectAnswerIndex. Your code snippet above has GetcorrectAnswerIndex but the message text you posted does not have Index.

here are the two scripts
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[] answerButtons;

                 int CorrectAnswerIndex;

[SerializeField] Sprite defaultAnswerSprite;

[SerializeField] Sprite correctAnswerSprite;

    //variables  

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)

{

    if(index == question.GetCorrectAnswerIndex())

    {

       

        questionText.text = "Correct!";

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

        buttonImage.sprite = correctAnswerSprite;

    }

}

}

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 Text Question "; // where you enter text question set as a string

[SerializeField] string answers = new string[4];

[SerializeField] int CorrectAnswerIndex;

public string GetQuestion()

{

  return question; //returns string question

}

 public string GetAnswer(int index)

 {

  return answers[index];

 }

public int GetAnswer()

{

  return CorrectAnswerIndex;

}

}

So there is your problem; You don’t have a GetCorrectAnswerIndex(). Yours is simply called GetAnswer(). So, the code should be

public void OnAnswerSelected(int index)
{
    if(index == question.GetAnswer()) // <-- This should just be GetAnswer()
    {
        questionText.text = "Correct!";
        Image buttonImage = answerButtons[index].GetComponent<Image>();
        buttonImage.sprite = correctAnswerSprite;
    }
}

Privacy & Terms