too much prefab for question (quiz games)

Hi, i currently making a quiz game like who want to be a millionaire, i have a problem… how can i shuffle the question (multiple answer), the answer ? without making too much prefab ? (so every question A out, the answer won’t always at A. , it answer can take place at B, C or D).

i thinking about making it this way

  • (1 question, 1 prefab), (1 answer, 1 prefab) if i make this, i will making 5 prefab for 1 question, if i have 100 question i must make 500 prefabs !

but i think it is not a good way, how to do it in a better way ? i don’t really want to have too much prefab :frowning: any leads will help me a lot… thanks

It sounds like you are not very familiar with Unity components and scripting. I advise you look a the tutorials.

You can do this by creating your own script to manage one instance of the prefab. The script can change the values shown on the “question” game object. You can call a method of the script when you want game object to show a new question.

You will need a way to store your questions and answers. I advise using XML (in a text file). You can find a tutorial on using XML with Unity here.

As an example, one of your XML nodes in your XML text file may look like this:

<Question>
  <questionText>What is the capital city of France?</questionText>
  <answerA>London</answerA>
  <answerB>Paris</answerB>
  <answerC>Leon</answerC>
  <answerD>Baguette</answerD>
  <correctAnswerID>1</correctAnswerID>
</Question>

A class to load the XML data may look like this (QuestionData.cs):

using UnityEngine;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System;

public struct Question {
    public string questionText;
    public string answerA;
    public string answerB;
    public string answerC;
    public string answerD;
    public int correctAnswerID;
}

[XmlRoot]
public class QuestionData {
    [XmlArray("Questions")]
    [XmlArrayItem("Question")]
    public List<Question>
        questions = new List<Question>();

    public static QuestionData LoadFromText(string text) {
        try {
            XmlSerializer serializer = new XmlSerializer(typeof(QuestionData));            
            return serializer.Deserialize(new StringReader(text)) as QuestionData;
        } catch (Exception e) {
            UnityEngine.Debug.LogError("Exception loading question data: " + e);
            return null;
        }
    }
}

A class to attach to the question game object might look like this (QuestionManager.cs):

using UnityEngine;
using System.Collections;

public class QuestionManager : MonoBehaviour {
    [SerializeField]
    private TextAsset questionDataXMLFile;
    private QuestionData questionData;
    private Question currentQuestion;

    void Start() {
        questionData = QuestionData.LoadFromText(questionDataXMLFile.text);
    }
	
    // Call this when you want a new question
    public void SetNewQuestion() {
        // gets a random question
        int q = Random.Range(0, questionData.questions.Count - 1);
        currentQuestion = questionData.questions[q];

        // add code here to set text values of your Question GameObject
        // e.g. GetComponent<SomeScript>().Text = currentQuestion.questionText;
    }

    // Use this to see if user selected correct answer
    public bool CorrectAnswerSelected(int selectedAnswerID) {
        return selectedAnswerID == currentQuestion.correctAnswerID;
    }
}

You can have a prefab for each question with an array of answer. When you pick the question you shuffle the answer:

// System picks questionA
GameObject obj = (GameObject)Instantiate(questionA);
QA script = obj.GetComponent<QA>();
script.ShuffleAnswers();
script.DisplayAnswers();

and in the QA script:

string [] answers = {"London", "Paris", "Berlin", "Bamako"}
public void ShuffleAnswers() 
{
    for (int i = answers.Length; i > 0; i--)
    {
        int j = UnityEngine.Random.Range(0,answers.Length);
        string s = answers[j];
        answers[j] = answers[i - 1];
        answers[i - 1] = s;
    }
  }
}

This is our new app related to the Quiz Game Chifro QuizOholic Lite is an Android-based quiz application providing you with a vast pool of knowledge in the form of quizzes on mobile phones. It is one of those apps that have no predefined target audience. You can find the quizzes deftly segregated under loads of categories ranging from traditional ones like English and Maths to offbeat ones like Fashion, Logos and Lifestyle…

play.google.com/store/apps/details?id=com.classteacher.quizoholiclite