Assigning 1 question to 1 cube? Screenshots shown.

Hi,

I have made a score system which works when you get answer a question correctly.

At the moment the player walks into the cube and a question appears, answer the question correctly then another question appears immediately after.

I have 10 questions and 10 cubes, is there away I can assign 1 question to 1 cube?

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

public class GUIScreen : MonoBehaviour
{
    public bool Ebola;
    public string Info;

    private List<string[]> questions  = new List<string[]>();
    private List<int> answerOrder = new List<int>(new int[] {1,2,3,4});

    private int count;
    public Text countText;


  
    void DrawInfo() {
        Rect rect = new Rect(500, 100, 400, 200);
        Rect close = new Rect(600, 500, 200, 100);
        if(GUI.Button(close, "CLICK")) {
            Ebola = !Ebola;
        }
      
        if (Ebola) {
            GUI.Box(rect, Info);
            GUI.Label(new Rect(520, 110, 400, 30), questions[0][0]);
            if (GUI.Button(new Rect(520, 200, 100, 30), questions[0][answerOrder[0]])) {
                HandleAnswer(answerOrder[0]);
            }
            if (GUI.Button(new Rect(520, 250, 100, 30), questions[0][answerOrder[1]])) {
                HandleAnswer(answerOrder[1]);
          
            }
            if (GUI.Button(new Rect(780, 200, 100, 30), questions[0][answerOrder[2]])) {
                HandleAnswer(answerOrder[2]);
          
            }
            if (GUI.Button(new Rect(780, 250, 100, 30), questions[0][answerOrder[3]])) {

                HandleAnswer(answerOrder[3]);
          
            }
      
        }
  
    }
  
    private void HandleAnswer(int answer) {
        if (answer == 1)
        {
            count = count + 1;
            SetCountText ();
            //Destroy (this.gameObject);

            //handle correct answer
            NextQuestion();


        }
        else {
            //Handle wrong answer
            //Destroy (this.gameObject);

      
        }
    }

    void SetCountText ()
    {
        countText.text = "Score: " + count.ToString ();
    }

  
    void OnGUI() {
        if(questions.Count > 0) {
            DrawInfo();
        }
    }
  
    void Start() {

        count = 0;
        SetCountText ();

        // String order: question, correct, wrong, wrong, wrong
        questions.Add(new string[] { "What year did Ebola begin?", "1976", "1986", "1996", "1966" });// Question 1
        NextQuestion();
        questions.Add(new string[] { "What is the Ebola virus named after?", "A river", " A farm", "A tree", "A city" }); // Question 2
        questions.Add(new string[] { "Which of these can pass along the ebola virus?", "Tears", "Dandruff", "Fingernails", "Soap" }); // Question 3
        questions.Add(new string[] { "Ebola is contagious only when someone has symptoms?", "True", "False", "", "" }); // Question 4
        questions.Add(new string[] { "Scientists think Ebola first came from?", "Bats", "Dogs", "Gorillas", "Mosquitoes" }); // Question 5
        questions.Add(new string[] { "Men who recover from Ebola should do this for 3 months?", "Wear condoms", "Test blood sugar", "Use private toilets", "Shower twice a day" }); // Question 6
        questions.Add(new string[] { "Which is the bigger threat: Ebola or the flu?", "Flu", "Ebola", "", "" }); // Question 7
        questions.Add(new string[] { "How many strains of the Ebola virus are there?", "Five", "One", "Three", "More than 100" }); // Question 8
        questions.Add(new string[] { "How long can the Ebola virus live on something outside the body?", "Up to 6 days", "Up to 6 hours", "Up to 6 minutes", "Up to 6 months" }); // Question 9
        questions.Add(new string[] { "How many proven treatments are there for Ebola?", "None", "One", "Two", "Three" }); // Question 10

        Shuffle(questions);
        Shuffle(answerOrder);
    }
  
    void NextQuestion() {
        questions.RemoveAt(0);
        Shuffle(answerOrder);
    }

    static readonly System.Random rng = new System.Random();
    public static void Shuffle<T>(IList<T> list) {
        int n = list.Count;
        while(n > 1) {
            n--;
            int k = rng.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
    }
}

HandleAnswers() seems to call NextQuestion()…

Or maybe you need to destroy/disable the cube, if it calls next question on collision/trigger?

Had a search online & watched videos but can’t seem to solve this problem?

private void HandleAnswer(int answer) {
        if (answer == 1)
        {
            count = count + 1;
            SetCountText ();
            Destroy (this.gameObject);

            //handle correct answer
            NextQuestion();

        }
        else {
            //Handle wrong answer
            Destroy (this.gameObject);
  
        }
    }

So at the moment player walks into cube, cube gets destroyed, UI box appears on screen with a question, answer question correctly, UI box disappears, 1 is added to score.

If answer is incorrectly, UI box disappears, score remains at 0.

How do i repeat this for all 10 cubes?

Anyone have any ideas or tutorials, videos, etc on this?

The easiest solution is to have an integer remember which question player had answered, and have a switch statement with that integer within the OnGUI to show just the question you wanted to ask for each cube.

You can also assign that integer into different cube to generate different cube with a single question asked.

Thanks for the reply, I’m very new to unity & programming c# so I’m not sure how to make an integer remember which question player had answered, and have a switch statement with that integer within the OnGUI to show just the question you wanted to ask for each cube.

Could you help me with this?

Since your code was fairly complicated, I’d thought you would be able to implement what I just said. Either way, I would break the problem into small trunk of task, and as you solve and struggle through them, post the code here and I will give hint as we go toward the solution. Below are just task, you should use the bold keywords to google for sample codes.

  • Make a static public integer field, so that you can read it across cubes, set it to 0 and as a question is prompted to the player, increase it by 1. This is to ensure that the next cube will be the desired next question.
  • Change the DrawInfo() method so that it will show only the question subjected to that integer you just created, basically you will have your questions[0][0] be changed into something e.g questions*[0] where i is the integer you just declared.*
    + Assign this script to all of your cube. And now test.
    Forget about the switch statement I mentioned since it’s kinda irrelevant. I thought you’re showing the questions using some if statement, but looks like you got a List.
    Anw, good luck.