Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by NoUJoe · Jun 19, 2014 at 03:17 AM · nullnullreference

Checking for null reference

Until about 10 minutes ago, I used to always check for null reference like so:

 if (someVariable)
 {
 
 //do stuff
 
 }

This has worked for me on every script I have ever wrote until now. The only thing I'm doing differently is checking if a variable of my own custom type is null whereas before I would always be checking if a unity type (collider, animator, rigidbody etc). I've done some googling but cannot even find examples where people check for null referencing the way I do so now I'm thinking how have I been able to do it so far. Any help?

Comment
Add comment · Show 3
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 Kiwasi · Jun 19, 2014 at 03:21 AM 0
Share

I'm guessing your custom type does not have a definition for the bool operator. Will get back to you on how to define one.

avatar image NoUJoe · Jun 19, 2014 at 03:27 AM 0
Share

Ahhh I see. I always thought that it was built into c#. Previous languages I've used have allowed if (variable) to check if an int is 1 or 0, a reference as null and a bool as true/false so I presumed c# would check for null and bool. Thanks for your help aswel, I appreciate it!

avatar image Kiwasi · Jun 19, 2014 at 03:35 AM 0
Share

As far as I've seen its a unity implementation.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Jun 19, 2014 at 03:40 AM

Try adding the following to your custom type. There may be a nicer way to code this. Untested, so no guarantee it will work.

 public static implicit operator bool(SomeVariable someVariable)
 {
     if (someVariable == null) {
         return false;
     } else {
         return true;
     }
 }

Edit:

This has been bouncing around in my head and I have two other solutions to propose.

  1. Inherit from ScriptableObject

  2. Change your checking logic to:

    if(someVariable != null)

Comment
Add comment · Show 6 · 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 Visal · Jun 19, 2014 at 04:05 AM 1
Share

use double equal sign ins$$anonymous$$d of a single equal sign if (someVariable == null)

and you can archive the same goal with a single line like this:

 return someVariable == null ? true : false;
avatar image Kiwasi · Jun 19, 2014 at 06:03 AM 0
Share

I vote that the next update to UA includes a check for compile time errors when you hit the code button.

Failing that I will edit in the second equals sign.

I knew there was a single line implementation, just not a syntax I commonly use. Thanks for adding it.

avatar image Visal · Jun 19, 2014 at 06:23 AM 0
Share

You're welcome :)

avatar image Eric5h5 · Jun 19, 2014 at 06:58 AM 1
Share

Just

 return someVariable == null;

is fine, no need for ternary operators.

avatar image Kiwasi · Jun 19, 2014 at 07:08 AM 0
Share

I told you there may have been a nicer way to code it. Thanks!

Show more comments

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

22 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

Related Questions

How to catch a NullReferenceException ? 3 Answers

Unity null check 1 Answer

Object Reference not set to Instance on Initializing Instatiation? 2 Answers

NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () 2 Answers

gameobject null 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