I have come across a strange bug. With the following code, I have setup an array of 15 strings to be equal to " ". However, when I check the values later in code, the first index gridContents[0] is equal to "" rather than " ". I manually check for this and set gridContents[0] = " "; but checking for it again later shows it reverts back to "". I am going to change the way the system works soon, but I was wondering if this is a known C# or unity thing to convert a space to an empty string in an array of strings.
The array is only ever initialized once and in searching the entire project, the only location of "" monodevelop can find is in my checking for it in the second snippet posted. Any help or information would be greatly appreciated. There is no larger context here, I am setting 15 string to " " and then later checking each of them and for some reason index 0 randomly appears to change to "".
String[] gridContents = new string[15];
for(int i = 0;i < 15;i++)
{
gridContents[i] = " ";
}
The second snippet here is used to correct the value, but later in code it reverts somehow.
for(int i = 0;i < gridContents.Length;i++)
{
if(gridContents[i].Equals(""))
{
Debug.Log("ERROR: empty string at index " + i);
gridContents[i] = " ";
}
}
--edit
Must be a unity / mono bug. The bug doesn't happen if I don't use " " as the default / empty space. The only "" mono can find in the entire project is where I check to see if any of the values are "", so it isn't being edited anywhere. It also doesn't occur until I use it to display text. The code is essentially, if the string is " " then display an empty grid. If it isn't, then display a white square in the grid. The grid is 3 rows of 5. I threw in checks all over the place and the bug doesn't show up until the very moment it is being checked to see if the string is " ".
Very strange bug, fixed by using a different character sequence than " " to denote an empty grid space.
--edit2
Problem solved. Strings in unity (maybe because of monobehaviors) default to an empty string instead of null. Some code I have to check if the user was holding something was not yet assigned when I first ran the program, so the variable was "" instead of null. And because "" is not " ", my grids registered a blank item. So for anyone in the future having this kind of issue... make sure your unity strings are all set to some value or null if they're supposed to be null. And even then, check it because a null string in unity prints "Null" haha.