Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Will Unity ever support nullable reference type in its library?

Discussion in '2023.1 Beta' started by Elmroot, Nov 6, 2022.

  1. Elmroot

    Elmroot

    Joined:
    Jan 3, 2022
    Posts:
    2
    I mean, e.g. GetComponent<T>() should return not just T, but T?, and other similar things.
    Is there even a plan to do that in the future?
     
    kdserra and NoTuxNoBux like this.
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    There is no difference when compared to the current API. All reference types have the possibility to be null by default, it would only make for things like TryGetComponent where if true it could have a hint for the IDE that output will not be null.
     
    plan-systems likes this.
  3. D_Kond

    D_Kond

    Joined:
    Mar 3, 2015
    Posts:
    25
    The point is that, there will be no warning for the example below, but it should be if nullables is enabled
    Code (CSharp):
    1. public class Card : MonoBehaviour
    2. {
    3.     [SerializeField] private Card _parent = null!;
    4.  
    5.     private void Awake()
    6.     {
    7.         _parent = transform.parent.GetComponent<Card>();
    8.     }
    9. }
     
    Last edited: Nov 6, 2022
    AldeRoberge and kdserra like this.
  4. atcarter714

    atcarter714

    Joined:
    Jul 25, 2021
    Posts:
    69
    Does this not do what you want? I use the compiler flag #nullable enable and I'm able to use nullables in whatever code I want, pretty much. Your original example code also shows the other way to get rid of the warnings by using the ! symbol at the end of a name/identifier, which is essentially telling the compiler "I know what I'm doing here, it'll be fine" and it just drops the issue and the warning is gone. I feel a little guilty for doing that sometimes and telling sweet Rosalyn to shut up, lol, but it's honestly a pretty valid way to get rid of an unhelpful warning message you don't need and when you're fully aware of what's going on and why she's nervous.

    Just try the flag I've mentioned above and demonstrated below and see if that isn't exactly what you want/needed though ...

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. #nullable enable
    5.  
    6. public class Card : MonoBehaviour
    7. {
    8.     [SerializeField] private Card? _parent = null;
    9.  
    10.     private void Awake() {
    11.         _parent = transform.parent.GetComponent<Card>();
    12.     }
    13. }
     
  5. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    811
    However, Unity C# is not compiled with nullable enabled, so all reference types are automatically nullable. Thus, with your own nullable enabled code, there are also null waring of methods that actually never return null. In addition, Unity also needs its own specific nullable settings that take into account Awake and Start. (And RequiredComponent attribute)
     
  6. D_Kond

    D_Kond

    Joined:
    Mar 3, 2015
    Posts:
    25
    Of course not :)
    private Card _parent = null! – It is just a way to suspend the erroneous warning for non-nullable field with the lack of constructor. And I hope that trick will be unnecessary in the future.
     
  7. NoTuxNoBux

    NoTuxNoBux

    Joined:
    Oct 2, 2020
    Posts:
    36
    The situation with Unity right now is indeed not ideal, even if you enable it yourself for your own code.

    Whilst semantically true right now, the plan for .NET is to move towards nullable reference types by default (in fact .NET 6 seems to have already enabled it by default), so Unity should follow suit at some point if they intend to keep up with .NET (which they seem to be working on), and having this in place sooner rather than later is beneficial as it allows gradual migration.
     
  8. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    811
    I just recently heard in a video on the dotnet channel on youtube, in .NET 7 the entire Framework now is compiled nullable enabled warning free. However, that only went with the help of the community due to the size of the framework.
     
    AldeRoberge and NoTuxNoBux like this.