Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by xoreel8 · Sep 11, 2016 at 02:19 AM · c#arraystringtextfield

Storing input to array

I am creating this quiz app and I'm having a little bit of trouble in storing the data and loading/displaying it afterwards. I will try to be as detailed as much as I can in explain my questions.

It's supposed to be this way

  • five text fields

  • 1 for question

  • 4 for choices

  • after user clicks save

  • the question will be stored in an array

  • new questions will be stored in the same array as the old questions

The way I structured the question and answers are like this:

  • Quiz is their parent

  • save button is not within quiz

I want to store/save the questions with its choices in an array so i can load them for future use.

EXTRA QUESTION:

Is there a way that I can randomize the order for the answers(when displayed)

For example inputs are

  • What is 2+2

  • 1

  • 2

  • 3

  • 4

Then it will display

  • What is 2+2

  • 3

  • 2

  • 4

  • 1

[EDIT]

Below is the class that accepts the user input for the question.

  public class NewQuestion{
 
     public static void getQuestion(string newQuestion)
     {
         Debug.Log("input" + newQuestion);
         string question = newQuestion;
     }
     
 }

Below is one of the classes that accepts the input for the answers.

 public class choiceA : MonoBehaviour
 { 
     public void getAnswerA(string A)
     {
         Debug.Log("input" + A);
         string optionA = A;
     }
 
 }

Below is the class where I am trying to call all the inputs when I click a save button.

  [System.Serializable]
         public class OnClickSave : MonoBehaviour
         {
         
             public string question;
             public string optionA;
             public string optionB;
             public string optionC;
             public string optionD;
         
             public ArrayList[] items;
 }
 



































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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Namey5 · Sep 11, 2016 at 07:08 AM

Thank you for updating your question. It's actually one of the better one's I've seen on this website, it was just a bit difficult to see where you were going. This is only part of the question answered, I'm just posting this here as an update;

 using UnityEngine;
 using System.Collections;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 [System.Serializable]
 public class Data
 {
     public int questionNumber;
     public string question;
     public string optionA;
     public string optionB;
     public string optionC;
     public string optionD;
 }
 
 public class DataTest : MonoBehaviour
 {
 
     public Data[] inputData;
 
     public Data[] data;
     
     // Update is called once per frame
     void Update ()
     {
         if (Input.GetButtonDown ("Fire1")) {
             data = new Data[inputData.Length];
 
             foreach (Data d in inputData)
                 Save (d, true);
         }
 
         if (Input.GetButtonDown ("Fire2"))
             data = Load ();
     }
 
     void Save (Data d, bool s)
     {
         data[d.questionNumber] = d;
 
         if (s)
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Create(Application.dataPath + "/data.dat");
             bf.Serialize(file, data);
             file.Close();
         }
     }
 
     Data[] Load ()
     {
         if (File.Exists (Application.dataPath + "/data.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter ();
             FileStream file = File.Open (Application.dataPath + "/data.dat", FileMode.Open);
             Data[] d = (Data[])bf.Deserialize (file);
             file.Close ();
             return d;
         }
         else
         {
             return new Data[1];
         }
     }
 }

This class should allow for the saving and loading of data. The great thing about this is, because we are using a custom class as a serializable container, we can save and load data as that format, meaning we don't have to do type conversions, etc. I'll work on the rest of the question when I have time.

Comment
Add comment · Show 3 · 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 xoreel8 · Sep 11, 2016 at 02:44 PM 0
Share

Forgive my lack of punctuation. This is a very big help. Thank you.

avatar image Namey5 xoreel8 · Sep 12, 2016 at 12:02 PM 0
Share

The answer has been updated. Again, thank you for updating the question, and I will work more on this when I have time.

avatar image xoreel8 · Sep 12, 2016 at 12:39 PM 0
Share

Thank you. It was late when I realized that there's a different rule with white spaces. :)

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

236 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 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 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 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 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 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 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 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 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# If string is equal to any in array 2 Answers

How to see if a string in a string array is equal to a normal string in C#? 1 Answer

trying to get an Array string to read an Int 0 Answers

Error CS1061: Are you missing an assembly reference? 2 Answers

How to make a random string from array in C#? 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