Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by leke · Jan 14, 2014 at 08:57 AM · arrays

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;
     }
     }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jan 14, 2014 at 09:12 AM 0
Share

The script compiled and attached to a game object just fine for me.

avatar image leke · Jan 14, 2014 at 09:33 AM 0
Share

i really don't understand whats going on then....cos its still giving me error

avatar image Eric5h5 · Jan 14, 2014 at 10:27 AM 0
Share

Start over with a new project and put this code in, and you'll find that does work with no errors.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jan 14, 2014 at 10:26 AM 0
Share

JS scripts are automatically classes that inherit from $$anonymous$$onoBehaviour, so that does not apply.

avatar image leke · Jan 14, 2014 at 11:17 AM 0
Share

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??

avatar image leke · Jan 14, 2014 at 11:25 AM 0
Share

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.

avatar image woodoo · Jan 14, 2014 at 11:25 AM 0
Share

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.

avatar image Eric5h5 · Jan 14, 2014 at 11:27 AM 0
Share

That's the issue...you don't want the script to be called Question because you already have another class called Question.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges