This is the code that I’m trying to retrieve a class from: I is attributed to an empty object called “Databases”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
//code will run when the game starts
public class CSVReader : MonoBehaviour
{
public TextAsset textAssetData;
[System.Serializable]
public class Question //grab data from the table, under these headings
{
public string Index;
public string StageQuestion;
public string StageAnswer;
public string DudAnswer1;
public string DudAnswer2;
public string DudAnswer3;
}
[System.Serializable]
public class QuestionList //create list of questions
{
public Question[] question;
}
public QuestionList TheQuestionList = new QuestionList();
//start is called before the first frame update
void Start()
{
Console.WriteLine("Start ReadCSV"); //functions starts
ReadCSV();
Console.WriteLine("End ReadCSV"); //function ends
}
void ReadCSV() //call function
{
string[] data = textAssetData.text.Split(new string[] { ",", "
" }, StringSplitOptions.None); //split csv into seperate data
int n;
int tableSize = data.Length / 6 - 1; //3 is the amount of columns
TheQuestionList.question = new Question[tableSize];
for(int i = 0; i < tableSize; i++)
{
TheQuestionList.question *= new Question(); //creating new question*
n = (6 * i) + 6 + 0; // index to the string
TheQuestionList.question*.Index = data[n]; //adding index value*
//Console.WriteLine(“i: {0}, n: {1}”, i, n);
n++; // = (6 * i) + 6 + 1; //n increments by 1
TheQuestionList.question*.StageQuestion = data[n]; //adding stage question*
//Console.WriteLine(“i: {0}, n: {1}”, i, n);
n++; // n = (6 * i) + 6 + 2; //n increments by 1
TheQuestionList.question*.StageAnswer = data[n]; //adding stage answer*
//Console.WriteLine(“i: {0}, n: {1}”, i, n);
n++; // n = (6 * i) + 6 + 2; //n increments by 1
TheQuestionList.question*.DudAnswer1 = data[n]; //adding dud answer 1*
//Console.WriteLine(“i: {0}, n: {1}”, i, n);
n++; // n = (6 * i) + 6 + 2; //n increments by 1
TheQuestionList.question*.DudAnswer2 = data[n]; //adding dud answer 2*
//Console.WriteLine(“i: {0}, n: {1}”, i, n);
n++; // n = (6 * i) + 6 + 2; //n increments by 1
TheQuestionList.question*.DudAnswer3 = data[n]; //adding dud answer 3*
//Console.WriteLine(“i: {0}, n: {1}”, i, n);
}
}
}
I’m using it to read a csv file that contains questions and answers for a quiz game I’m trying to write. I want to reference the questions and display them to a text box, but I am struggling to figure out how to reference the classes, as there are multiple.
This is what I’ve got currently, i’m new to C# so i’m sorry for this code!! I’ve looked around for answers but nothing is really working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class QuestionPull : MonoBehaviour
{
//empty container
CSVReader csvReader;
[SerializeField] GameObject Databases;
public Text questionText;
void Awake()
{
csvReader = Databases.GetComponent();
}
//called before first frame
void Start()
{
csvReader.Question = questionText;
questionText.text = Question[1].StageQuestion;
}
}
TL;DR I’m trying to get the “StageQuestion” bit into the QuestionPull script so that I can get questions to display to the screen in a text box.
Thank u!
P.S if theres anything I can do better, please lmk!!