Changing the text on 4 different buttons depending on the picture displayed

Hi! I was wondering if it is possible to change the text of four buttons depending upon the picture shown. I am trying to create a guessing game that displays a random screenshot of a game and has four different options. Currently I have made a script for a random image to appear on screen. The pictures are stored in an array. So my question is, is it possible to make a script to change the text of the buttons dependent on the position in the array and if so how would I go about it. I have been stumped on this for a while. Thanks in advance!

Example: A picture of Rocket League appears and there are four buttons one with the correct answer and three with the incorrect answer.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// This is my code for generating a random picture

public class LoadRandomPicV2 : MonoBehaviour {


    public Texture2D pic0;
    public Texture2D pic1;
    public Texture2D pic2;
    public int pick;

    Texture2D[] pics;

    // Use this for initialization
    void Start() {
        pick = Random.Range(0, 3);

        pics = new Texture2D[3];
        pics[0] = pic0;
        pics[1] = pic1;
        pics[2] = pic2;
}

    private void OnGUI()
    {
        GUI.DrawTexture(new Rect(0, 3, 334, 212), pics[pick]);
    }

  

}

I would make a new serializable class and declare an instance of a list for it so you can access the image and the answers, then display them, like so:

[Serializable]
public class Trivia
{
    public string question;
    public List<string> answers;
    public Sprite image;
}

Once you have that class, instantiate it in your trivia manager monobehaviour class like so:
public List triviaQuestions;

Then add all the questions with images and answers via inspector on the object with the script.

Then on your trivia display class you can go through your list like soL
to access the question do triviaQuestions[0].question
to access the answers do triviaQuestions[0].answers[0], replacing 0 by the iterator of which trivia or answer you want to fetch.

Also, on a side note, I’d suggest not using OnGUI and using canvas instead.

1 Like