0

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.

  • 5
    Arrays do not mutate themselves. 2 options: a) some code not shown is manipulating the array, or b) at some point you are talking about a different array – Marc Gravell Dec 29 '13 at 21:49
  • 2
    http://i.imgur.com/qbGmPTy.png interesting, ha ? – Selman Genç Dec 29 '13 at 21:53
  • I'm assuming it's unity related. If I press the key to show the grid, the error doesn't show until the code tries to check the value of each grid. Very strange, but for the moment fixed. – user2118704 Dec 29 '13 at 23:08
  • This is not Unity related did you tried with == ? check that answer : http://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals – Géry Arduino Dec 30 '13 at 12:30
  • No, it wasn't an issue of == vs .Equals(). I finally narrowed it down. When switching between screens, one of my variables to determine if a grid was empty was being set to null. And some code was still trying to access it. So instead of giving null, it gave the empty string. And since "" is not " ", it started causing the bug where some grids had empty items in them. --edit just to clarify c# strings are null by default, monobehavior / unity strings are empty by default. That's why I didn't notice this sooner. – user2118704 Dec 31 '13 at 17:31

0 Answers0