- Home /
Unity c# StreamReader to list always returns false.
Hello. 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();
sr.Close();
var lines = fileContents.Split("\n"[0]);
foreach (string line in lines)
{
IngameItems.Add(line);
}
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. :D
Also, Is there a better way to read and save data from a list in unity?
Thank you.
Jesper
Try this
if (IngameItems[0] == "ACW-R") print("Found ACW-R");
Answer by MartinCA · Jan 01, 2014 at 09:37 PM
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("\n"[0]);
Try using Split( @"\\n" ), or Split( Environment.NewLine )
Your answer
Follow this Question
Related Questions
PlayerPrefs script problem 2 Answers
Issues with string array taken from a .txt file 2 Answers
Why can't I save an array? 1 Answer
How can I save and read data on Android? 1 Answer
How can I serialize the Particles of a Particle Emitter 1 Answer