I am trying to use list for the first time :-) I have the list defined in a class:
using UnityEngine;
using System.Collections;
using System.IO;
using UnityEditor;
using System; //This allows the IComparable Interface
using System.Collections.Generic;
public class Words : MonoBehaviour {
public List<string> wordslist = new List<string>();
static void WriteString()
{
string path = "Assets/Resources/words.txt";
//Write some text to the test.txt file
StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine("Test");
writer.Close();
//Re-import the file to update the reference in the editor
AssetDatabase.ImportAsset(path);
TextAsset asset = (TextAsset)Resources.Load("words");
//Print the text from the file
Debug.Log(asset.text);
}
static void ReadString()
{
string text = " ";
string path = "Assets/Resources/words.txt";
//List<WordsList> wordslist = new List<WordsList>();
int ix = 1;
//Read the text from directly from the test.txt file
StreamReader reader = new StreamReader(path);
while(text != null){
text = reader.ReadLine();
if (text != null) {
wordslist.Add (text);
ix++;
Debug.Log (wordslist[ix].ToString());
}
}
//Debug.Log (reader.ReadToEnd ().Length);
//Debug.Log(reader.ReadToEnd());
reader.Close();
}
public void GetWord(){
ReadString ();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
I am trying to add words from a text file in to the list and display the list in the console.
the reading fails:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
I am trying to access the list because I want to add code to select a random word from the list after this works
I am having trouble defining it correctly and reading the values after the Add function. I didn't try the WriteString yet, in case you see something wrong there too