Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nyonge · Jan 29, 2020 at 08:33 PM · variablenulltypenanextension-methods

Checking if variable is nullable type?

How do I check, not if a variable is null, but if it's a nullable type?

I want to make an extension function for a null comparison that applies to every variable that's possibly nullable, so if I have something like

 BoxCollider col;

I can just check if it's null with

 col.IsNull();

However, if I check for null on say an int or float, that'll throw an error since ints are non-nullable, closest would be some IsNan stuff. How can I make a general IsNull check across all potentially nullable variables, and further, how do I know inherently if a variable is of a nullable type? Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by nyonge · Jan 29, 2020 at 09:05 PM

Think I figured it out, will post for reference for anyone else. Nullable doesn't apply to basic .net data types like int or float, but is a property of all UnityEngine objects like Vectors and MonoBehaviours.

I made some extension methods to check if a single variable is null, or if two objects are mismatched null types (one null, the other not):

     /// <summary>
     /// Is this object null or unassigned? Returns true if so
     /// </summary>
     /// <param name="o">this object to check</param>
     /// <returns>true if null</returns>
     public static bool IsNull(this Object o) {
         if (safeNullCheck && (!o || o == null))
             return true;
         return !o;
     }
     private const bool safeNullCheck = false;
 
     /// <summary>
     /// Returns true if one object's IsNull state does not match the other's
     /// </summary>
     /// <param name="o">this object to check</param>
     /// <param name="obj">Object to compare against this one</param>
     /// <returns>true if objects null states do not match</returns>
     public static bool IsNullMismatched(this Object o, Object obj) {
         if (o.IsNull ()) {
             if (obj.IsNull ())
                 return false;
             return true;
         }
         return !obj.IsNull ();
     }
 
     /// <summary>
     /// Returns true if one object's IsNull state does not match the other's. Outputs a bool specifiying if both objects are null.
     /// </summary>
     /// <param name="o">this object to check</param>
     /// <param name="obj">Object to compare against this one</param>
     /// <param name="areBothNull">True if both objects are null, false if either is non-null</param>
     /// <returns>true if objects null states do not match</returns>
     public static bool IsNullMismatched(this Object o, Object obj, out bool areBothNull) {
         if (o.IsNull ()) {
             if (obj.IsNull ()) {
                 areBothNull = true;
                 return false;
             }
             areBothNull = false;
             return true;
         }
         areBothNull = false;
         return !obj.IsNull ();
     }

Which should cover what I'm looking for, and should work for all data types that extend from UnityEngine.Object. Props to Jovanni Cutigni for going into more detail about nullable info on their site. (archived)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Kishotta · Jan 29, 2020 at 09:17 PM

According to this StackOverflow question, you could use an extension method to make this easily accessible.

 public static class NullableExtensions {
     public static bool IsNull<T> (this T obj) {
         if (obj == null) return true; // obvious

         Type type = typeof (T);
         if (!type.IsValueType) return true; // reference type
         if (Nullable.GetUnderlyingType (type) != null) return true; // Nullable<T>
         return false; // value type
     }
 }

Usage:

 private void Awake () {
     if (_boxCollider.IsNull ()) {
         // _boxCollider is both nullable and is actually null
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by sacredgeometry · Feb 02, 2020 at 01:13 PM

Like these?

default(Type) == null !typeof(Type).isValueType


or this


!myObj.GetType().isValueType

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to use a variable as a Type? 1 Answer

String Variable From External Script Set To Null? 2 Answers

What is typing a variable? Is it assigning the value? 3 Answers

empty component var 1 Answer

Resources.LoadAll Issue with variable type 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges