Complete C# Unity Game Developer 2D: Lesson 59 - Swapping Sprites

In the Quiz Master tutorial, the instructor shows how to code the buttons to give correct or incorrect answers, and that is what I am trying to achieve. The word ‘sprite’ at the bottom of my code received a red squiggly line. I italicized/asterisked it in the code below. I tried to experiment with the code and looked at the Unity docs before asking here. The error says:

“Image does not contain a definition for ‘sprite’ and no accessible extension method ‘sprite’ accepting a first argument of type ‘Image’ could be found (are you missing a directive or an assembly reference?).”

public class RotG : 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;
        }
    } 
}

Hi @hhatton93,

Please hover your mouse over Image in your script editor. Does the popup window mention UnityEngine.UI or another namespace? ‘Image’ is a very common name that exists in myriad namespaces. It might be that your script editor found another Image class in another namespace, and that Image class is not Unity’s Image class.

In the code you shared, I cannot see any namespace defined at the top of your code. Please check your script and compare it with the top of Gary’s script. You need using UnityEngine.UI;.

I hope this helped. :slight_smile:


See also:

1 Like

I don’t see Gary using UnityEngine.UI, but I added that namespace and it did remove the error. I finished the lesson and added the rest of the code. Nothing happens when I click the answer buttons though.

using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class RotG : 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)
    {
        Image buttonImage;

        if (index == question.GetCorrectAnswerIndex())

        {

            questionText.text = "Correct";
            buttonImage = answerButtons[index].GetComponent<UnityEngine.UI.Image>();
            buttonImage.sprite = correctAnswerSprite;
        }
        else
        {
            correctAnswerIndex = question.GetCorrectAnswerIndex();
            string correctAnswer = question.GetAnswer(correctAnswerIndex);
            questionText.text = "Sorry, the correct answer was;\n" + correctAnswer;
            buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite;
        }
    }
} ```

Did you connect the OnAnswerSelected method with your Button components? See the OnClick() field in the Button components.

I did miss that step, and it allowed the incorrect and correct messages to show up. However, it gives the “Correct” response for the incorrect answer. I’m not sure how to make the game able to discern between an incorrect and correct answer. I tried moving the sprites around. Maybe the code needs to be updated with a different image for the correct answer statement?

The game is not intelligent. It does not know what we consider ‘correct’ or ‘incorrect’. The game simply shows a string in the game window depending on the value of the correctAnswerIndex in the current QuestionSO object.

You click a button. The button passes on an integer to your program. The program checks if that integer is equal the aforementioned correctAnswerIndex.

And based on the result of this evaluation, it shows a string and swaps sprites.

If something is not correct according to your human definition, use Debug.Logs to figure out what’s going on at runtime. The code gets executed within milliseconds, and a lot happens in Unity behind the scenes. It is often impossible to tell what happens just by looking at one class.


Maybe the code needs to be updated with a different image for the correct answer statement?

Yes, that could be the case but that’s an assumption, not knowledge. Neither you nor I are clairvoyants, aren’t we? However, an assumption is very helpful when you want to debug. Check whether your assumption is correct.

We have two ‘logics’, if you will: one for the text (string) and one for the sprites. If the text is correct, focus on the sprites.

1 Like

I completely forgot about the scriptable objects which contain the indexes. Thanks for pointing me in the right direction. Resolved.

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

Privacy & Terms