0

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

  • Try put the increment like this : Debug.Log (wordslist[ix].ToString()); ix++; – imsome1 Aug 16 '17 at 08:08
  • 1
    You're trying to access a non-static field within a static method. – Ryan Searle Aug 16 '17 at 08:19
  • Unrelated to your current problem but I just want to point to you to a big **future** problem. This code will not work in a build. If it does in a Windows build, it will **never** work on mobile build. You **cannot** write to the Resources folder because it is read-on. You can read/write to `Application.persistentDataPath/YourFolder` path which works on all platforms. See [this](https://stackoverflow.com/a/40966346/3785314) for example. – Programmer Aug 16 '17 at 14:17

2 Answers2

1

All indexes start with 0 not 1, so replace

int ix = 1;

with

int ix = 0;

and move the ix++; to the very last line in the while-loop, so after:

Debug.Log (wordslist[ix]);
ix++;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • thanks, but the problem still exists. also I have Assets/Scripts/Words.cs(41,33): error CS0120: An object reference is required to access non-static member `Words.wordslist' – Daniel Ben-Shabtay Aug 16 '17 at 08:15
  • @DanielBen-Shabtay: have you also moved the `ix++` after the line where you use it? Otherwise it will be 1 and if you only have one item that causes the exception because `wordslist[1]` accesses the second item – Tim Schmelter Aug 16 '17 at 08:16
  • @DanielBen-Shabtay: and you have also started with 0 not 1? – Tim Schmelter Aug 16 '17 at 08:21
0

Do you really want you ReadString and WriteString methods to be static? Having static methods means they are not tied to an instance of your Words class meaning you cannot use the field wordslist as this belongs to an instance of your Words class.

You could pass the instance of wordslist as a parameter to the ReadString instead.

Ryan Searle
  • 1,597
  • 1
  • 19
  • 30
  • I don't mind passing the wordslist as a parameter to ReadString and WriteString. All I need is to be able to read the text file to the list, select a random word. I am not sure I will need the WriteString, I've just copied it from the sample just in case I need it – Daniel Ben-Shabtay Aug 16 '17 at 08:40
  • I mean if this method is only going to be called for the purpose of this class it may as well be non-static and forgo the extra parameter. – Ryan Searle Aug 16 '17 at 08:45