QuestionSO' does not contain a definition for 'GetAnswer'

Hi i am getting this error and unsure how to fix it thanks

Assets\Scripts\Quiz.cs(17,36): error CS1061: ‘QuestionSO’ does not contain a definition for ‘GetAnswer’ and no accessible extension method ‘GetAnswer’ accepting a first argument of type ‘QuestionSO’ could be found (are you missing a using directive or an assembly reference?)

questionSO script

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 GetCorrectAnswer(int index)

{

    return answers[index];

}

public int GetAnswerIndex()

{

    return correctAnswerIndex;

}

}

Quiz script

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using TMPro;

public class Quiz : MonoBehaviour

{

[SerializeField] TextMeshProUGUI questionText;

[SerializeField] QuestionSO question;

[SerializeField] GameObject[] answerButtons;

void Start()

{

    questionText.text = question.GetQuestion();

    TextMeshProUGUI buttonText = answerButtons[0].GetComponentInChildren<TextMeshProUGUI>();

    buttonText.text = question.GetAnswer(0);

}

}

The error says that there’s nothing called GetAnswer inside your QuestionSO class. To fix this check your grammar in both classes, or create the method GetAnswer in your QuestionSO class, if needed, to fix your issue.

Click here for the solution, but try to solve it on your own for a better learning experience.

Two ways to solve this:

  1. In your QuestionSO class, change the method GetCorrectAnswer to GetAnswer.
    or
  2. In your Quiz class, change this line of code
    buttonText.text = question.GetAnswer(0);
    to
    buttonText.text = question.GetCorrectAnswer(0);.

Hope this helps.

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

Privacy & Terms