OnAnswerSelected is not showing up for me

I can’t get OnAnswerSelected to show up for me in the drop down tab for my buttons, I have the image type sliced, I’ve looked over my code. A few things are different for me than in the instructors videos. When he has to hold “control + .” over image to add unityUI, I did not, it automatically added “using Microsoft.Unity.VisualStudio.Editor;” on its own. Below are my two codes. Please Help
This is my quiz script

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using TMPro;

using Microsoft.Unity.VisualStudio.Editor;

public class Quiz : MonoBehaviour

{

[SerializeField] TextMeshProUGUI questionText;

[SerializeField] QuestionSO question;

[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)

{

    if(index == question.GetCorrectAnswerIndex())

      {

         questionText.text = "Correct!";

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

         buttonImage.sprite = correctAnswerSprite;

      }

}

}

This is my QuestionSO script

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

[CreateAssetMenu(menuName = “Quiz Question”, fileName = “NewQuestion”)]

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;

 }

}

This is wrong. Remove this and do the Ctrl + . like in the video. You should not have a reference to Microsoft.Unity.VisualStudio.Editor.

Make sure you drag the Quiz component (from the hierachy) into the button’s OnClick event. I don’t have the Quiz Master project on this machine so I faked it. It should look something like this

I already had the Quiz script drug down into the OnClick() area, I removed “using Microsoft.Unity.VisualStudio.Editor;” and added using UnityEngine.UI; now not only do I not see OnAnswerSelect but only see one option and all other options are now gone. I posted a screen shot below

image

Below is my Quiz script after "Using UnityEngine.UI; but it’s still not showing On Answer Selected in the drop down area of OnClick() after I drag the quiz script to it.

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;

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;

      }

}

}

Make sure to assign a game object to the field. That game object needs a Quiz component. Assigning the Quiz script does not work.

Compare the little icon in bixarrio’s screenshot with yours. Here is what bixarrio has:

image

1 Like

Nina you are actually a Legend and the Hero of my day. When I went back and redid everything I was just going so fast that I thought I was doing something right when I wasnt…Opps. This is all very new to me and I appreciate gamedev.tv and all of you so much for your help. Thank you very much. Doing these courses has really been helping me a lot and making me feel better about myself. Thank you all.

You’re welcome but you also need to thank bixarrio because he’s often waaay faster than I, and also very helpful. If he hadn’t shared his screenshot, you wouldn’t have posted yours, and I probably wouldn’t have guessed right. :wink:

It’s great to read that you are enjoying our courses. If you ever feel bad because something does not work as seen in the video, remind yourself of the fact that Rick, Gary, I, and everybody else were beginners, too, at some point. We might not be beginners anymore but we still make mistakes and get stuck. You just don’t see that in the videos because nobody wants to watch an instructor looking for a bug for hours. The mistake you made here happened to basically everybody I know (including me). Of course, I remember most of the mistakes I made, and I also remember the solution most of the time.

1 Like

Thank you for the kind words and yes, Thank you very much bixarrio. All of you are amazing.

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

Privacy & Terms