-1

I am making a quiz with both unity and c#. At the end there is a survey with toggle questions and an in input field and I am using arrays to save the data from the survey. Is there any way I can take the whatever I store in my array and put it in a text file so I can open it later? Here is my code for the survey:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIScript07 : MonoBehaviour
{
    public GameObject[] questionGroupArr;
    public QAClass07[] qaArr;
    public GameObject AnwerPanel;
    void Start()
    {
        qaArr = new QAClass07[questionGroupArr.Length];
    }

    
    void Update()
    {

    }
    public void SubmitAnswer()
    {
        for (int i = 0; i < qaArr.Length; i++)
       {
           qaArr[i] = ReadQuestionAndAnswer(questionGroupArr[i]);
        }
    }
     QAClass07 ReadQuestionAndAnswer(GameObject questionGroup)
    {
        QAClass07 result = new QAClass07();
        GameObject q = questionGroup.transform.Find("Question").gameObject;
        GameObject a = questionGroup.transform.Find("Answer").gameObject;

        result.Question = q.GetComponent<Text>().text;

        if (a.GetComponent<InputField>() != null)
        {
           result.Answer = a.transform.Find("Text").GetComponent<Text>().text;
        }
        else if (a.GetComponent<InputField>()== null)
        {
            string s = "";
            int counter = 0;

            for (int i = 0; i < a.transform.childCount; i++)
            {
                if (a.transform.GetChild(i).GetComponent<Toggle>().isOn)
                {
                    if (counter != 0)
                    {
                        s = s + ",";
                    }
                    s = s + a.transform.GetChild(i).Find("Label").GetComponent<Text>().text;
                    counter++;

                }
                if (i == a.transform.childCount - 1)
                {
                    s = s + ".";
                }
            }
            result.Answer = s;

        }
        return result;
    }

    [System.Serializable]
    public class QAClass07
    {
        public string Question = "";
        public string Answer = "";
    }
}
  • 3
    Yes, you can write to a text file. Have you done any research? – Crowcoder Feb 20 '21 at 13:22
  • Duplicate of e.g. [Saving and loading data in Unity](https://stackoverflow.com/questions/40078490/saving-loading-data-in-unity) – derHugo Feb 20 '21 at 16:06
  • 1
    Does this answer your question? [Saving/loading data in Unity](https://stackoverflow.com/questions/40078490/saving-loading-data-in-unity) – Gugu72 Feb 20 '21 at 16:09

1 Answers1

0

You can save your array data as json object and save it as text file. If you want your custom classes to save to json too, you should add **[System.Serializable] which you added already. Then for saving any serialized object you can create a serialized class that you can add every variable. Here is sample workflow.

[Serializable]
public class SaveObject
{
    public GameObject[] questionGroup;
    public string playerName;
    public QAClass07[] qaArr;
    public GameObject AnswerPanel;
}

[System.Serializable]
public class QAClass07
{
    public string Question = "";
    public string Answer = "";
}

public void Save()
{
    var saveObject = new SaveObject()
    {
        AnswerPanel = GetAnswerPanel(),
        playerName = GetPlayerName(),
        qaArr = GetqaArr,
        questionGroup = GetquestionGroup
    };

    string saveText = JsonUtility.ToJson(saveObject);
    File.WriteAllText(Application.persistentDataPath + "/SAVES/save.txt", saveText);
}

public void LoadQ()
{
    if (File.Exists(Application.persistentDataPath + "/SAVES/save.txt"))
    {
        isLoading = true;
        string saveText = File.ReadAllText(Application.persistentDataPath + "/SAVES/save.txt");

        var saveObject = JsonUtility.FromJson<SaveObject>(saveText);

        // You can load back all objects from saveObject in same way. Load and Get methods are random. You get the point
        LoadAnswerPanel(saveObject.AnswerPanel);
    }
}

The reason there is a class named SaveObject is collecting all data in one class and save to json and load it back from json easily.

SeLeCtRa
  • 600
  • 1
  • 6
  • 14