Image instead of text for scriptable object

Is there a way to have my “question” be an image instead of text? I want to make a Pokemon guessing game. I tried looking in the manual, but nothing stuck out to me.

Sure. Instead of having a string for the question, change it to a Sprite. You will then need sprites for each question. You’d also remove the text field from UI and use an Image instead. Now, when it’s time to show the question, assign the sprite from the question to the image’s sprite field. Job’s done

this is the code I have. I have the field for me to put an image in the question, but it’s not switching images when I change questions.

This is fine. Where’s the code that sets the question?

This is the code I have for the Quiz.cs

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

public class Quiz : MonoBehaviour
{

    [Header("Questions")]
    [SerializeField] Sprite questionImage;
    [SerializeField] TextMeshProUGUI questionText;
    [SerializeField] List<QuestionSO> questions = new List<QuestionSO>();
    QuestionSO currentQuestion;

    [Header("Answers")]
    [SerializeField] GameObject[] answerButtons;
    int correctAnswerIndex;
    bool hasAnsweredEarly;

    [Header("Button Colors")]
    [SerializeField] Sprite defaultAnswerSprite;
    [SerializeField] Sprite correctAnswerSprite;
    [Header("Timer")]
    [SerializeField] Image timerImage;
    Timer timer;
    void Start()
    {
        timer = FindObjectOfType<Timer>();
        DisplayQuestion();
    }
    void Update() 
    {
        timerImage.fillAmount = timer.fillFraction;
        if(timer.loadNextQuestion)
        {
            hasAnsweredEarly = false;
            GetNextQuestion();
            timer.loadNextQuestion = false;
        }
        else if(!hasAnsweredEarly && !timer.isAnsweringQuestion)
        {
            DisplayAnswer(-1);
            SetButtonState(false);
        }
    }
    public void OnAnswerSelected(int index)
    {
        hasAnsweredEarly = true;
        DisplayAnswer(index);
        SetButtonState(false);
        timer.CancelTimer();
    }

void GetNextQuestion()
{
    SetButtonState(true);
    SetDefaultButtonSprite();
    GetRandomQuestion();
    DisplayQuestion();
}

void GetRandomQuestion()
{
    int index = Random.Range(0, questions.Count);
    currentQuestion = questions[index];

    if(questions.Contains(currentQuestion))
    {
    questions.Remove(currentQuestion);
    }
}
void DisplayAnswer(int index)
{
    Image buttonImage;
        if(index == currentQuestion.GetCorrect())
        {
            questionText.text = "Correct!";
            buttonImage = answerButtons[index].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite;
        }
        else
        {
            correctAnswerIndex = currentQuestion.GetCorrect();
            string correctAnswer = currentQuestion.GetAnswer(correctAnswerIndex);
            questionText.text = "Sorry, the correct answer was:\n" + correctAnswer;
            buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();
            buttonImage.sprite = correctAnswerSprite;
            
        }
}
void DisplayQuestion()
{
            questionImage = currentQuestion.GetQuestion();


        for(int i = 0; i < answerButtons.Length; i++)
        {
            questionText.text = "Correct!";
            TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
            buttonText.text = currentQuestion.GetAnswer(i);
        }

       
    }

    void SetButtonState(bool state)
    {
        for(int i = 0; i < answerButtons.Length; i++)
        {
            Button button = answerButtons[i].GetComponent<Button>();
            button.interactable = state;
        }
    }

    void SetDefaultButtonSprite()
    {
        for(int i = 0; i < answerButtons.Length; i++)
        {
            Image buttonImage = answerButtons[i].GetComponent<Image>();
            buttonImage.sprite = defaultAnswerSprite;
        }
    }

}

This should be an Image on the UI

[SerializeField] Image questionImage;

Then, in DisplayQuestion you will do this

void DisplayQuestion()
{
    questionImage.sprite = currentQuestion.GetQuestion();

    for(int i = 0; i < answerButtons.Length; i++)
    {
        questionText.text = "Correct!";
        TextMeshProUGUI buttonText = answerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
        buttonText.text = currentQuestion.GetAnswer(i);
    }
}

thank you so much! That fixed the problem.

It’s a pleasure

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

Privacy & Terms