Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by MKpuniek, Nov 27, 2023.

  1. MKpuniek

    MKpuniek

    Joined:
    May 22, 2022
    Posts:
    2
    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)
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,015
    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.
     
  3. MKpuniek

    MKpuniek

    Joined:
    May 22, 2022
    Posts:
    2
    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.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,015
    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! :)
     
    SisusCo likes this.
  5. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,259
    Something like this should also work for suppressing the warning for all types in a single assembly.
    Code (CSharp):
    1. [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.