Serialized object's string field is null

So what is the reason this happens.

I have serialized C# classes for items. I’ve been playing with sort of inventory system once again.

[System.Serializable]
public class Item
{
   public string typename;
   public int slotID;
   // other int, float and string fields
}

Elsewhere, in inventory view class, I try to clear the slots if they are empty.

These fields are (at least for now) serialized so that I can edit them, so these are seldom null (but have empty fields I guess because it’s serialized and shown in inspector), despite of this, I’ll first check if it’s null.

But then comes the thing I’m wondering…

I have to check if “typename” field - which is just string, if it’s null. Otherwise I’ll get error that object is null, if I try to just check length of the typename string.

if (itemData == null)
{
    IsFull = false;
}
else if (itemData.typename == null || itemData.typename.Length < 1)
{
    // Without null check, error on line above.
    IsFull = false;
}
else
{
// ...

I’ve haven’t done much with serialized objects in inspector, so maybe this is common… so far I’ve thought one would have to specifically make a string nullable for it to be able to be null. Unless I’ve botched something obvious.

Yeah, you’ll have to check if it’s null, it’d be like trying to use a null GameObject variable.

I’m a little tired I may not have comprehended properly

I mean, it would be the same if you just made a string variable in a monobehavior script and didn’t give it a value.

EDIT:

wait, sorry, so You have to check if Itemdata as a whole is null? That’s weird, it shouldn’t be null at all.

I’m assuming itemdata is like this;

Item itemData = new Item();

@DeeJayVee

Thanks for the reply, my point is:

Is this some Unity editor serialization thing I’m not aware of. Null might be the default for string, but seems like the item data isn’t null, the if check passes, so the Item class instance seems to be there, but then the fields (or at least this string field) isn’t filled run time. And I’ll get the error if I don’t check this.

So even if I do itemData = null, then proceed to inventory view… the itemData no longer is null, but the fields might be. This is what I’m trying to understand.

Strings can be null. Instead of this:

if (itemData == null)
{
    IsFull = false;
}
else if (itemData.typename == null || itemData.typename.Length < 1)
{
    IsFull = false;
}

try this:

if (itemData == null || string.IsNullOrEmpty(itemData.typename))
{
    IsFull = false;
}
2 Likes

@TonyLi

Thanks, I’ll do what you said. Already kind of figured that I have to do this, although had kind of forgot isNullOrEmpty exists, as I haven’t really had need to use it that much yet.

However, I’m still wondering, why Unity does it like this. I understand that serialized object will show up in inspector, but why the strings are left null, where as when the class is already filled/left empty in editor, the fields never are null… or so I’ve observed so far… I might be wrong.

As far as I understand it (which, i’m no pro) The serialization just allows the properties to be modified in the editor and quote “Survive an assembly reload”.

If Unity deserializes an object containing a null string, it will be an empty string. But if you’ve created it yourself (e.g., new ItemData()), then it will be null.

3 Likes

I know this post is old, but it was the only one I found about this topic.
I have a similar problem, but the other way, I want my string field to be null, but Unity will always set it to string empty instead.
My string is private and does not have the SerializedField (or any other) attribute, so as far as I understand Unity should ignore the field during serialization logic, but somehow it is still set to empty instead of null. Is there any way to keep the null value of a private string field in a ScriptableObject.

Default value of a string is null everywhere else in C# so it took me a while to find this “bug” in my code (I compared the string to null in my code and then realized that it was set to empty instead, even with a explicit init value of null).

“Private fields are serialized under some circumstances (editor).”
Source: Unity Blog

2 Likes

Thanks for the link, the NonSerialized attribute fixed my issue.