- Home /
Multiple Choice Game of Questions stored in an array
pls help!! I'm trying to create a multiple choice kinda game where the user wud b able to answer some questions shown on the screen. Now here's the problem, i want to store a sizable amount of questions (and answers) in arrays bcos there wud be a lot of them and coding them wud take forever. and i got a code online of how i cud get that done but when i tried to attach the script to my main camera, it gives me an error description of "Cant add script behaviour question, The script needs to derive from monobehaviour" pls i'm new to unity and i dont knw what this error means. the code compiled well in monoDevelop but can't be attached to any game object. Pls any help wud be greatly appreciated. Thanks in advance
Here's the code i used
#pragma strict
var arrayOfQuestions : Question [];
class Question
{
var questionText : String;
var answerText : String;
/* example constructor (creates a Question from two params) */
function Question ( q : String, a : String )
{
questionText = q;
answerText = a;
}
}
/* initialize the question array in the start function */
function Start ()
{
arrayOfQuestions = new Question [20];
/* use a for loop to create dummy questions */
for ( var i = 0; i < 20; i ++ ) {
var someQ : String = "Question number: " + i;
var someA : String = "Answer number: " + i;
/* use the example constructor from above */
var freshQuestion : Question = new Question(someQ, someA);
arrayOfQuestions[i] = freshQuestion;
}
}
/* test it */
function OnGUI ()
{
var labelRect : Rect = Rect(200, 200, 300, 30);
for ( var thisQ : Question in arrayOfQuestions ) {
var txt = "Q: " + thisQ.questionText + " A: " + thisQ.answerText;
GUI.Label(labelRect, txt);
labelRect.y += labelRect.height;
}
}
The script compiled and attached to a game object just fine for me.
i really don't understand whats going on then....cos its still giving me error
Start over with a new project and put this code in, and you'll find that does work with no errors.
Answer by woodoo · Jan 14, 2014 at 10:01 AM
It is saying that you need to inherit from Monobehaviour, example:
public Question : Monobehaviour
{
...
}
Every script that you will attach to a GameObject needs to inherit from Monobehaviour, so you can override methods/functions like Start, Awake, Update, etc.
Note that the code above is written in C# and not JS.
Hope it helps.
JS scripts are automatically classes that inherit from $$anonymous$$onoBehaviour, so that does not apply.
i'm confused here. is it a javascript or c#? and secondly. i tried opening a new project and putting d script and it still didnt work....pls guys any other suggestions??
it now works but only when i changed the javascript name. before i named it "Question" but now i just left the default name. Thanks guys i really appreciate the comments.
The error description given by him says: "Cant add script behaviour question, The script needs to derive from monobehaviour".. Sorry if i didn't realize it, but probably is something related to that.
That's the issue...you don't want the script to be called Question because you already have another class called Question.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Array empties values on RunTime? 1 Answer
For loop not running correctly inside start () 1 Answer
transform array in javascript 2 Answers
Add a copy of a object to Array 1 Answer