string.Length and/or stringbuilder.Length always returning 0

Hey there,

I’m currently stuck on something I can’t wrap my head around, would someone be able to explain why this debug.Log returns 0 as a value, throwing a nullreference directly after.

string test = "";
        for (int i = 0; i < names.lastNames.Count; i++)
        {
            if (names.lastNames[i].lastName.prefix != "") //if the prefix is not empty
            {
                test += names.lastNames[i].lastName.prefix; //add it

                Debug.Log(test.Length); // Returns 0 ?
                if (test[test.Length - 1] == ' ') // Throws nullreference
......

The same happens when trying to do this with stringbuilder.Append, stringbuilder.Length returns 0 and nullreference on the stringbuilder.Length when trying to check it.

What happens if you print out the value of “names.lastNames*.lastName.prefix”?*

1 Like

Well thanks appearently that threw a null. I was convinced that prefix was always either “” or some actual string, turns out it was able to be null as well.