- Home /
(FIXED) TextAsset.name is never equal to compared string
so here's the problem. i'm trying to load text assets based on a list of file names (in this case its simple config files for weapons). first off i was loading them from an external file using a streamReader, which was working perfectly and still does. now, i'm trying to do the same thing using textAssets imported into unity. the problem is that when i try to compare the 'name' of some of the textAssets to the list of names that i have, they never return true. no matter what.
i've been pulling my hair out for hours over a simple: is "weapon" == "weapon"? nope. i've debugged the hell out of it and the strings i am trying to compare are 100% equal, yet they never return true.
here is part of my code:
for (int a = 0; a < SaveManager.defaultSaves.Length; a++){
Debug.Log(SaveManager.defaultSaves[a].name + "_" + fileName);
if(SaveManager.defaultSaves[a].name == fileName){
there are definitely no extra spaces or anything like that. this is driving me insane. worst part is that it actually does work in other scenarios. just not this one for god knows what reason.
i can't even really think of what an answer to this would even be, there seems to be no reason as to why these aren't working.
here's some more of the code:
private static bool Load(string fileName, bool useDefault){
if(!useDefault){
try{
string line;
string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string file = dir + @"\data\" + fileName + ".txt";
StreamReader theReader = new StreamReader(file, Encoding.Default);
using (theReader){
do{
line = theReader.ReadLine();
if (line != null){
if(line.Length != 0){
if(line[0] != '/')
SaveManager.data.Add(line);
}
}
}
while (line != null);
theReader.Close();
return true;
}
}
catch (IOException e){
Debug.Log("" + e.Message);
return false;
}
}
else{
for (int a = 0; a < SaveManager.defaultSaves.Length; a++){
Debug.Log(SaveManager.defaultSaves[a].name + "_" + fileName);
if(SaveManager.defaultSaves[a].name.ToString() == fileName.ToString()){
string[] lines = SaveManager.defaultSaves[a].text.Split("\n"[0]);
for (int i = 0; i < lines.Length; i++){
if (lines[i] != null){
if(lines[i].Length != 0){
if(lines[i][0] != '/')
SaveManager.data.Add(lines[i]);
}
}
}
return true;
}
}
Debug.Log("file not found: " + fileName);
return false;
}
}
FIXED!
i added a new line of code:
lines[i] = lines[i].Trim();
turns out it problem was caused \r characters. using Trim() removed any \r characters and fixed the issue.
Use .ToString() for each item you want a String for, if it doesn't already give you one.
adding ToString() didn't make a difference. i even tried adding (string) before each variable and even breaking them both down into an array or chars and back to strings again.
also just to clarify, in the example 'defaultSaves' is an array of textAssets, and 'fileName' is a string parameter for the function this is in.
Debug.Log inside a for doesn't really work and will only seem to kick out one result. You can't rely on it to debug a for loop. What comes after your if statement?
Not sure if this is the issue, but you are not stripping out carriage returns. You are only stripping out newlines. So depending on how the text was authored, you may have a hidden character in each string. Try splitting on both '\r' and '\n' and discarding empty lines (StringSplitOptions.RemoveEmptyEntries).
Your answer
Follow this Question
Related Questions
Looking for a javascript unity implementation of FromCharCode? 0 Answers
Separating two strings 1 Answer
Need help (String) 2 Answers
Reading integer value from specified character(s) on text line? 2 Answers