- Home /
Default string value... empty? (C#)
I've declared a public array of strings in my class without assigning values to them. I intend to enter them for each object in Unity's inspector pane. I won't always use the full array (some objects will have less options associated with them,) so I loop through it, and when it finds the first null element in the array it sets int usableLength to that index and stops looping.
Strangely, I found that usableLength always remained at 0. But if I use if (array[x].Equals("")), or IsNullOrEmpty for that matter, it works. I thought C# was supposed to initialize all types to null by default? It's not preventing me from making progress now that I've figured it out, but I'm curious as to why.
If these arrays are filled in the inspector, why do you end up having empty values in the first place? Adjust the array length in the inspector and fill all items, not easier and safer?
I hadn't known you could change the array size in the inspector until I actually dragged my script onto an object. It could be argued that I have to do slightly less work for each object this way, but you're pretty much right... Still, I want to know about this.
I guess this is just a matter of Unity developers decision. $$anonymous$$aybe they decided empty strings are more intuitive?
If you have a variable available in inspector, then you probably want to fill it with some real value. For me, empty string is such a value, while null is not.
I guess it was a decision to allow easy work with the inspector. Otherwise, you would have needed another control near that area to say "null". In any case, good or bad, it can be easily worked around.
Also, I guess you know that you can expose arrays of your own objects in the inspector, or an array of enums, so all in all I think there are enough ways to solve each use case.
Good point. Thanks guys.
Come to think of it, by not entering anything in the inspector, it's probably "filling" the array elements with nothing.
Answer by DannyB · Sep 10, 2013 at 11:26 AM
I believe this is what is going on:
Uninitialized strings in C# will have a null default value.
Uninitialized public strings in Unity (or rather, in a MonoBehavior) will have an empty value.
This demonstrates:
string myString;
string[] myStrings;
public string myPublic;
public string[] myPublics;
void Start() {
myStrings = new string[2];
Debug.Log(myString); // null
Debug.Log(myStrings[0]); // null
Debug.Log(myPublic); // ""
Debug.Log(myPublics[0]); // ""
}
And, as discussed in the comments, I would try to avoid having arrays with "holes". You can either use the inspector array size, or if it does not need to be inspector-exposed, use a generic List instead.
Finally - if you still prefer (or must) keep an array with holes, then you already found your answer. It would be item == ""
if its a public inspector array, or item == null
and in any case, you better off using IsNullOrEmpty
.
Wow that's interesting. I had a bug that was an implication of Unity setting public strings to another default value than the one I had given it. Thank you for spotting it for me!
Answer by bezier · Jul 28, 2021 at 03:08 AM
Monobehaviour's public fields will have serialization once they get involved in some prefabs. That's why they got objects assigned when you check them in Editor or Player.
Your answer
Follow this Question
Related Questions
Public variables null after assigned in the inspector. 3 Answers
Assigning Texture2D to script 1 Answer
Setting a variable to nothing 1 Answer
How to initialize non-static field damage into string? 2 Answers
TextMeshPro " != " not working 1 Answer