- Home /
I am doing a VR experiment for which I have to display words from a csv file onto the canvas with a gap of 5 seconds between each word,
I have a csv file that looks somewhat like this: index,word,rate,..... 1,bat,... 2,ant,... 3,rat,... These words bat, ant, rat should be displayed on the canvas (not the console) for an experiment. Each word should be displayed with a gap of 5 seconds (first as soon as I press play, bat needs to be displayed and 5 seconds later ant and so on.). But the problem is my code only displays the last word rat on the screen and no other word. I am new to Unity so I don't know where I am going wrong. Here is the code I wrote: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Loadquests : MonoBehaviour { List quests = new List(); float timer = 0; int i; // Start is called before the first frame update void Start() { TextAsset Book = Resources.Load("Book"); string[] data = Book.text.Split(new char[] { '\n' }); for (int i = 1; i < data.Length - 1; i++) { string[] row = data[i].Split(new char[] {','});
Quest q = new Quest();
int.TryParse(row[0], out q.index);
q.word = row[1];
float.TryParse(row[2], out q.brightness);
float.TryParse(row[3], out q.distance);
quests.Add(q);
}
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
TextAsset Book = Resources.Load<TextAsset>("Book");
string[] data = Book.text.Split(new char[] { '\n' });
if (timer> 2 && i < data.Length - 1 )
{
i += 1;
timer = 0;
foreach (Quest q in quests)
{
GetComponent<TextMesh>().text = q.word;
}
}
} }
Quest is another c# script that goes:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Quest { public int index; public string word; public float brightness; public float distance; }
Any help would be appreciated. Thank you very much! ,
Your answer
Follow this Question
Related Questions
ReadLine to a Variable 2 Answers
Add asset to array during runtime 0 Answers
csv to unity 0 Answers
Using an Excel generated .csv file 1 Answer