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 JoshMBeyer · Dec 12, 2014 at 02:14 AM · c#listaccess

Last time I'll ask for help on this lol

I have 2 scripts. Questionnaire and QuestionList. The questionnaire script contains the gameplay and questionlist is the well list of course. I got it all to work fine in 1 single script. But as you can imagine that would take forever to scroll through thousands of questions to get to the code for debuging so I decided to move them to there own script QuestionList. I need help accessing the list on QuestionList from the Questionnaire script. Its basicly grabing questions from the list and displaying them. Please let me know what I'm doing wrong here.

Script 1

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Questionnaire : MonoBehaviour {
 
     IEnumerator Wait()
     {
         GUILayout.Label("Correct!");
         yield return new WaitForSeconds(2.5f);
         //....
         
     }
     public int index;
     
     class Question
     {
         public string QuestionText;
         public List<string> Answers = new List<string>();
         public int CorrectAnswer;
         
     }
 
     
 
     void Awake()
     {
   
         //Get a question at runtime.
         GetQuestion();
     }
 
     Question GetQuestion()
     {
         List<Question> aList = new List<Question>();
         
 
         string question = this.gameObject.GetComponent<QuestionList>().list[index].QuestionText;
         int rand = Random.Range(0, aList.Count);
         index = rand;
         return aList[rand];  
     }
 
     void OnGUI()
     {
         //--------------------------For Debug Purposes--------------------------------
         GUI.Label(new Rect(10, 10, 100, 20), "Questions: " + aList.Count);
         GUI.Label(new Rect(10, 30, 200, 20), "Current Question: " + index.ToString());
         GUI.Label(new Rect(10, 60, 200, 20), "Current Answers: " + aList[index].Answers);
         //-----------------------------------------------------------------------------
 
         //Create the GUILayout area.
         GUILayout.BeginArea(new Rect((Screen.width - 200) / 2, (Screen.height - 200) / 2, 200, 200));
 
         //Display a question.
         GUILayout.BeginVertical();
         GUILayout.Label(aList[index].QuestionText);
        
         //Display the answers.
         GUILayout.BeginHorizontal();
         for (int i = 0; i <= 3; i++ )
         {
             if (GUILayout.Button(aList[index].Answers[i]))
             {
                 //If correct answer...
                 if (i == aList[index].CorrectAnswer)
                 {
                     Debug.Log("Correct");
 
                     if (index < aList.Count)
                     {
                         aList.Remove(aList[index]);
                         GetQuestion();
                     }    //Here we would add the Ending...
                     else { Debug.LogError("Out Of Questions To Display"); }
                 }
                 else //if incorrect....
                 {
                     Debug.Log("Incorrect");
                     GetQuestion();
                     
                 }
             }
         }
         GUILayout.EndHorizontal();
         GUILayout.Space(10);
         GUILayout.EndVertical();
         GUILayout.EndArea();
     }
 
    
     
 }
 
 

And script 2...

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class QuestionList : MonoBehaviour {
 
     
     public class Question
     {
         public string QuestionText;
         public List<string> Answers = new List<string>();
         public int CorrectAnswer;
     }
    
     public List<Question> list = new List<Question>();
     
     void Awake()
     {
         //Questions and answers are listed here. Index beginning at 0.
         //The CorrectAnswer property should set to the correct
         //Index of the correct answer in this first one you can see
         //Correct answer is index "3". 0, 1, 2, and 3("Four Text").
         list.Add(new Question());
         list[0].QuestionText = "What is 2 + 2?";
         list[0].Answers.Add("1");
         list[0].Answers.Add("2");
         list[0].Answers.Add("22");
         list[0].Answers.Add("4");
         list[0].CorrectAnswer = 3;
 
         list.Add(new Question());
         list[1].QuestionText = "What is 4 + 4?";
         list[1].Answers.Add("4");
         list[1].Answers.Add("44");
         list[1].Answers.Add("8");
         list[1].Answers.Add("100");
         list[1].CorrectAnswer = 2;
 
         list.Add(new Question());
         list[2].QuestionText = "What is 6 + 6?";
         list[2].Answers.Add("16");
         list[2].Answers.Add("9");
         list[2].Answers.Add("12");
         list[2].Answers.Add("6,000,000");
         list[2].CorrectAnswer = 2;
 
         list.Add(new Question());
         list[3].QuestionText = "What is 8 + 8?";
         list[3].Answers.Add("2");
         list[3].Answers.Add("88");
         list[3].Answers.Add("5");
         list[3].Answers.Add("16");
         list[3].CorrectAnswer = 3;
 
         
     }
 }
 

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by NicRule · Dec 12, 2014 at 03:48 AM

Interesting game. I found 8+8 particularly intriguing.

Here is the Questionnaire script with a lot cut out to make it easier to see what I changed:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Questionnaire : MonoBehaviour {
 
     public int index;
 
     List<QuestionList.Question> aList = new List<QuestionList.Question>();
     
     void Awake()
     {        
         //Get a question at runtime.
         aList = QuestionList.fill();
         GetQuestion();
     }
 
     void GetQuestion()
     {
         index = Random.Range(0, aList.Count);
         return;
     }
 
     void OnGUI()
     {
         if (aList.Count == 0)
         {return;}
         //--------------------------For Debug Purposes--------------------------------
         GUI.Label(new Rect(10, 10, 100, 20), "Questions: " + aList.Count);
         GUI.Label(new Rect(10, 30, 200, 20), "Current Question: " + index.ToString());
         GUI.Label(new Rect(10, 60, 200, 20), "Current Answers: " + aList[index].CorrectAnswer);
         //-----------------------------------------------------------------------------
         
         //Create the GUILayout area.
         GUILayout.BeginArea(new Rect((Screen.width - 200) / 2, (Screen.height - 200) / 2, 200, 200));
         //Display a question.
         GUILayout.BeginVertical();
         GUILayout.Label(aList[index].QuestionText);
         //Display the answers.
         GUILayout.BeginHorizontal();
         for (int i = 0; i <= 3; i++ )
         {
             if (GUILayout.Button(aList[index].Answers[i]))
             {
                 //If correct answer...
                 if (i == aList[index].CorrectAnswer)
                 {
                     Debug.Log("Correct");
                     if (index < aList.Count)
                     {
                         aList.Remove(aList[index]);
                         GetQuestion();
                     }    //Here we would add the Ending...
                     else {
                         Debug.LogError("Out Of Questions To Display");                    
                     }
                 }
                 else //if incorrect....
                 {
                     Debug.Log("Incorrect");
                     GetQuestion();
                 }
             }
         }
         GUILayout.EndHorizontal();
         GUILayout.Space(10);
         GUILayout.EndVertical();
         GUILayout.EndArea();
     }    
 }


And the other script

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class QuestionList : MonoBehaviour {
     
     public class Question
     {
         public string QuestionText;
         public List<string> Answers = new List<string>();
         public int CorrectAnswer;
     }
 
     static public List<Question> fill()
     {
         List<Question> list = new List<Question>();
 
             list.Add(new Question());
             list[0].QuestionText = "What is 2 + 2?";
         int remove = Random.Range(0, 3);
         for (int i = 0; i < 4; i++){
 
 
             list[0].Answers.Add("1");
 
             list[0].Answers.Add("2");
 
             list[0].Answers.Add("22");
 
             list[0].Answers.Add("4");
             list[0].CorrectAnswer = 3;
         }
             list.Add(new Question());
             list[1].QuestionText = "What is 4 + 4?";
             list[1].Answers.Add("4");
             list[1].Answers.Add("44");
             list[1].Answers.Add("8");
             list[1].Answers.Add("100");
             list[1].CorrectAnswer = 2;
 
             list.Add(new Question());
             list[2].QuestionText = "What is 6 + 6?";
             list[2].Answers.Add("16");
             list[2].Answers.Add("9");
             list[2].Answers.Add("12");
             list[2].Answers.Add("6,000,000");
             list[2].CorrectAnswer = 2;
 
             list.Add(new Question());
             list[3].QuestionText = "What is 8 + 8?";
             list[3].Answers.Add("2");
             list[3].Answers.Add("88");
             list[3].Answers.Add("5");
             list[3].Answers.Add("16");
             list[3].CorrectAnswer = 3;
 
         return list;
     }
 }


[Summary of changes]

I call the class through "QuestionList.Question" to access the class from the other script. The "Question" class in the original was a completely independent class in each script, so they were not compatible.

The "fill()" function has a return type of "List" so I set "aList" equal to it to import the information.

I added if (aList.Count == 0) return; to the start of the GUI section to avoid getting an infinite list of ArgumentOutOfRange errors when you run out of questions.

"GetQuestion" could probably be replaced since I cut it down to just one function but I wanted to make it easier to read.

Best of luck. I'll check back if anyone has any questions.

Comment
Add comment · Show 7 · 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 JoshMBeyer · Dec 12, 2014 at 04:24 AM 0
Share

Greatly appreciate the response. I tried to replace the void Awake like you said with public static List fill() but there were errors because "List" wasn't recognized .So I changed it too static public List fill() and some errors were fixed but "list" was having errors. I know that I filled it out wrong. Can you post the QuestionList?

I see that if you type < then something and > again it deletes that from your comment? So I am assu$$anonymous$$g yours did too

avatar image NicRule · Dec 12, 2014 at 05:30 AM 0
Share

I updated my answer. Sorry I didn't include both scripts it the original post. Also thanks for mentioning the < deletion thing. It's nice to know about, but I did not have that problem.

avatar image JoshMBeyer · Dec 12, 2014 at 06:49 AM 0
Share

its saying QuestionList.fill(); takes "0" arguements?

avatar image JoshMBeyer · Dec 12, 2014 at 06:59 AM 0
Share

Also if you know of a way to "randomize" the order the answers are displayed let me know of any idea's. Since I am using a forloop to toss a button 1,2,3,4 i am assu$$anonymous$$g i have to create a new method ins$$anonymous$$d of using a forloop

avatar image JoshMBeyer · Dec 12, 2014 at 07:24 AM 0
Share

and on the aList = QuestionList.fill(); I see that it says it takes a question and an int. SO i tried filling that out and used (aList, i) and it says Cannot implicitly convert type void' to System.Collections.Generic.List'

Show more comments
avatar image
0

Answer by Daniel Greenhorn · Dec 12, 2014 at 07:24 AM

I would have done this with 2 arrays , 1 for the texts and the other for the numbers. Same rows for the same question. myarrayofstrings [index, 0] = "what is 4+4" myarrayofnumbers [index, 0] = 8; myarrayofnumbers [index, 1] = 2 and as many variants as you need An if to myarrayofnumbers [index, 0] determines if the answer is correct. Add a random range for each question and another for the order in which the options are displayed

Problem is to compile the long , long, LONG array but if you know a little VBA in Excel or OpenOffice, it can can be easily done there and pasted in Start(). Make the table and let the macro do the work. From 20+ cases upwords it's exhilatating!

At least that's how I've done something similar. No need to write cases till you're old :)

While at it, you could not use either cases or arrays. Have random numbers from 0 to 9 as the arguments of each question and display in a random order the correct answer and random numbers from 0 to 18 for the false answers

Comment
Add comment · 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

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# How to get/access List from another script 1 Answer

Distribute terrain in zones 3 Answers

How to access List<> from other script? 1 Answer

Accessing Smartfox room list from another script in C# 1 Answer

JS to C# List type problem 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