- Home /
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 :( any leads will help me a lot.. thanks
Answer by hamstar · Feb 14, 2014 at 10:02 AM
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;
}
}
Hi, thanks for your answer... i am currently developing my 2nd game with unity3d, i never use xml before...
in your example, the question and the answer is in plain text. if my question and my answer is gameobject(texture), so the structure in hierarchy is like this question{answer1,answer2,answer3,answer4}
can i do this via xml ? i was thinking about placing collider in every answer and get the gameobject name to check if the answer is correct or not.
So each question has it's own texture? You could do it like that but then there's a lot of textures. Wouldn't it be easier to using Unity GUI or another GUI system?
The question could be a label, and the answers buttons. Then in SetNewQuestion()
above, you just have to change the text of the label and buttons using the values from currentQuestion
.
You could add something like this to Question$$anonymous$$anager.
private string answerAText;
void OnGUI() {
// answer A
if (GUI.Button(new Rect(10, 10, 150, 100), answerAText)) {
// answer A has id "0"
if(CorrectAnswerSelected(0)) {
// user got it right!
}
else {
// user got it wrong!
}
}
}
yes, the question is always an image, how can i do this in good way then ?
its like i have an apple, oranges and strawberry... that is all an image, the answer is text so apparently i can do code for answer like you said, but about the question, i don't have any leads :(
If I understood correct, maybe this would work?
1) In X$$anonymous$$L change questionText to questionImage with the name for the image file. e.g.
<questionImageName>FranceCaptial.jpg</questionImageName>
2) Save all your question images in your Resources folder.
3) Have a Question$$anonymous$$aterial $$anonymous$$aterial you use on your game object to render question image.
4) In SetNewQuestion()
, change the texture for Question$$anonymous$$aterial. e.g.
Texture2D texture = Resources.Load(currentQuestion.questionImageName) as Texture2D;
questionImageRenderer.material.mainTexture = texture;
I catch the script and xml, but not work here!! I have this error when i drag the prefab for my hierarchy:
Exception loading question data: System.InvalidOperationException: There is an error in X$$anonymous$$L document. ---> System.InvalidOperationException: was not expected at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlType$$anonymous$$apping root$$anonymous$$ap) [0x00000] in :0 at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () [0x00000] in :0 at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x00000] in :0 --- End of inner exception stack trace --- at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x00000] in :0 at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.XmlReader xmlReader) [0x00000] in :0 at System.Xml.Serialization.XmlSerializer.Deserialize (System.IO.TextReader textReader) [0x00000] in :0 at QuestionData.LoadFromText (System.String text) [0x00010] in D:\ProjetosUnity\QuestionGame\Assets\Quiz\QuestionData.cs:27 UnityEngine.Debug:LogError(Object) QuestionData:LoadFromText(String) (at Assets/Quiz/QuestionData.cs:29) Question$$anonymous$$anager:Start() (at Assets/Quiz/Question$$anonymous$$anager.cs:12)
Someone help-me ?
Answer by fafase · Feb 14, 2014 at 09:43 AM
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;
}
}
}
Answer by sahiloffice · Nov 21, 2014 at 01:12 PM
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