Unity c# StreamReader to list always returns false.

Can someone please help me with a script?

I use StreamReader to read and write from an inventory list to a .txt, but every time i compare an item in the list, it just returns false no matter what.
There is no Errors ans no Exceptions.

The “reading to the list part” works fine, i get a nice list in the unity inspector, but when i compare the Elements of the list to other strings, it returns false, even though I am 100% positive it is exactly the same.

My script is as follows:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

public List<string> IngameItems = new List<string>();
    void Start ()
         var sr = new StreamReader(Application.dataPath + "/" + "Resources/Items.txt");
         var fileContents = sr.ReadToEnd();
         var lines = fileContents.Split("


    	foreach (string line in lines) 
         if (IngameItems[0] == "ACW-R")
         print("Found ACW-R");

It never prints: “Found ACW-R”.

There is definitely an “ACW-R” in the first slot of the list. None missed space like " ACW-R" or "ACW-R " or anything like that.

I have tried a lot of different things, so please help me if you can.
There is not a lot of data on StreamReader and StreamWriter for c#, so I would be most delighted if you could link me to some pages explaining just this. :smiley:

Also, Is there a better way to read and save data from a list in unity?

Thank you.


First off, I’m gonna be nitpickish and comment about the unreasonable use of inferred typing.

Second of all, whats up with this index access at 0?

var lines = fileContents.Split("


Try using Split( @"
" ), or Split( Environment.NewLine )