Handling "Non-nullable serialised field 'object' is uninitialized. Consider declaring the serial"

I’m working on a project with enabled Nullables and “Non-nullable serialised field ‘object’ is uninitialized. Consider declaring the serialised field as nullable” warning is apearing in the console when I declare serialised variable.
Is there any possible way of handling apart from declaring “GameObject object = null!” and to do it globally on a whole project?

(excuse my grammar it’s my first post)

The whole point of nullables is to get such warning and fix them by expressly assigning null. Which I find really stupid because null is the default value anyway.

You can probably disable the warning by adding this to the top of every file that causes this warning:
#pragma warning disable XXXX

whereas XXXX is the “CS0123” warning number (just the number, not the prefix letters). Use the actual number from the warning message, 0123 is just an example.

Is there a way to make a plugin that will add this line to every serialized file I have and I will have in my project ?
There’s a lot of files in it so doing it manually would be time consuming.

It may be configurable in the IDE. Look for “suppress warnings” or similar. But this may be a per-project config and with Unity recreating the .csproj files it won’t stick. I think you can use a csc.rsp file for this though, see here at the bottom. Not sure about the exact syntax for this.

Alternative: configure the IDE to automatically clean up code such that fields get explicit initializers added. I love code cleanup taking care of all this manual labor stuff. I never push brackets or commas around with spaces and tabs anymore, or deleting empty lines and such. I just write (really) dirty code and have it cleaned up on a keypress. That frees up an unbelievable amount of mental load you wouldn’t believe until you experienced it yourself! :slight_smile:

1 Like

Something like this should also work for suppressing the warning for all types in a single assembly.

[assembly: SuppressMessage("Nullable reference types", "CS8618")]

But csc.rsp is even more flexible, since it can be applied only to certain directories even within an assembly.

Note that this could of course also end up hiding warnings that would actually have been helpful to see.

For example, if you attach a component at runtime using AddComponent, all reference type fields will have null values after initialization, unless you manually assigned something to them in the field initializers / constructor.

And for types that don’t derive from UnityEngine.Object, the chance is increased further.