String != Same String

I call this under Start():

		CharacterDictionary = new Dictionary<string, Texture2D>(){
		{"Character1", Character1},
		{"Character2", Character2},
		{"Character3", Character3},
		{"Character4", Character4},
		{"Character5", Character5},
		{"Character6", Character6},
		{"Character7", Character7},
		{"Character8", Character8},
		{"Character9", Character9},
		{"Character10", Character10},
		{"Character11", Character11},
		{"Character12", Character12},
		{"Character13", Character13},
		{"Character14", Character14},
		{"Character15", Character15},
		{"Character16", Character16},
		{"Character17", Character17},
		{"Character18", Character18},
		{"Character19", Character19},
		{"Character20", Character20}
		};

I call this under Update():

				if (lines[lineNum].StartsWith("-")||lines[lineNum].StartsWith("+")){
				if (lines[lineNum].StartsWith("+")){
					CharSearch = lines[lineNum].TrimStart('+');
					if (CharacterDictionary.ContainsKey(CharSearch)){
						CharRight = CharacterDictionary[CharSearch];
					}else{
						Debug.Log(CharSearch);
						StartNextLine = true;
					}
				}else{
					CharSearch = lines[lineNum].TrimStart('-');
					if (CharacterDictionary.ContainsKey(CharSearch)){
						CharLeft = CharacterDictionary[CharSearch];
					}else{
						Debug.Log(CharSearch);
						StartNextLine = true;
					}
				}

The code seems to be doing its job, since substituting CharSearch with “Character1” gets me the relevant Texture2D. However, for some reason, even when CharSearch is the string “Character1”, it still returns false. For testing, the two strings it starts off by checking are:

+Character1

and

-Character2

Both of which were pulled as strings from a text file. They return Character1 and Character2 from Debug.Log respectively.

I also tried this:

				if (lines[lineNum].StartsWith("-")||lines[lineNum].StartsWith("+")){
				if (lines[lineNum].StartsWith("+")){
					CharSearch = lines[lineNum].TrimStart('+');
					if (CharSearch == "Character1"){
						CharRight = CharacterDictionary[CharSearch];
					}else{
						Debug.Log(CharSearch);
						StartNextLine = true;
					}
				}else{
					CharSearch = lines[lineNum].TrimStart('-');
					if (CharSearch == "Character2"){
						CharLeft = CharacterDictionary[CharSearch];
					}else{
						Debug.Log(CharSearch);
						StartNextLine = true;
					}
				}

Those also returned false.

What’s the problem?

Your problem is that CharSearch doesn’t contain “Character1”. There are propably some almost invisible characters at the end. Maybe a space to linefeed character or something like that. How does your text-file look like and how do you split it into lines?

Try to print out the length of your search string.

Debug.Log(">" + CharSearch + "< (" + CharSearch.Length + ")");

If it contains “Character1” this should print out ">Character1< (10)":

While it is true that string is reference type, it isn’t handled as reference type. You don’t copy string references, only the containing value unless it’s a ref-parameter.

Using a dictionary with string keys works just fine. C# works way different to C++.

You should improve your text parsing. There is a Trim function which will remove spaces on both ends of the string. I would do it like this:

    string line = lines[lineNum].Replace("

“,”“).Replace(”\r",“”).Trim();
if (line.StartsWith(“+”))
{
CharSearch = line.TrimStart(‘+’);
if (CharacterDictionary.ContainsKey(CharSearch))
{
CharRight = CharacterDictionary[CharSearch];
}
else
{
Debug.Log(“>” + CharSearch + “< (” + CharSearch.Length + “)”);
StartNextLine = true;
}
}
else if(line.StartsWith(“-”))
{
CharSearch = line.TrimStart(‘-’);
if (CharacterDictionary.ContainsKey(CharSearch))
{
CharLeft = CharacterDictionary[CharSearch];
}
else
{
Debug.Log(“>” + CharSearch + “< (” + CharSearch.Length + “)”);
StartNextLine = true;
}
}

But without the information how your textfile looks like, if there is additional information in the same line, we can’t help you optimising the parsing-process.

You have to understand that string1 == string2 only works, because the == operator is overloaded (in c#) and compares every char in the strings. In other words it compares the values of the strings for equality

However, the dictionary does not do that. It will only accept a key if it is the exact same object, or in other words the reference you give it is pointing to the same object. That is not the case with two different string objects even if they have the same value. And with if (CharSearch == “Character1”) you only compare the value.

So like stated in the comment, I would not recommend using strings for keys of your dictionary.