Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Inok · Apr 23, 2016 at 09:48 PM · null reference exception

Null Reference Exeption harmless?

I have real situation when i get Null Reference Exeption and this not break anything in gameplay:

 void OnTriggerEnter2D(Collider2D coll)
   {
       var tags = coll.gameObject.GetComponent<Tag_Obj>().Tags;
       for (int i = 0; i < tags.Length; i++)
       {
         if (tags[i] == Tag_Obj.Tag.WHEEL)
         {
           // do staff
         }
       }
   }

I implement my own multi tag system by simply make tag class and put here array of enum to represent tag names instead slow string that used in compareTag call. Ideally i need to check for null component Tag_Obj before access it but then i need to call Getcomponent twice! (first for check for null, second for cache reference to local variable(for perfomance reason, this way i reduce GetComponent calls)). So i consider not do null check to reduce GetComponent call to only 1 time. And skip of null check produce null reference exeption when object that have trigger collider contact with this object but not have tag component. So question: Null reference exeption harmless by itself? I mean it not do memory leaks or something bad like that?

Comment
Add comment · Show 2
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 gjf · Apr 23, 2016 at 10:28 PM 0
Share

the exception should be considered bad.

there's little performance impact to checking whether the component is null before using it, especially compared to time taken to get the component in the first place!

you shouldn't need to call GetComponent() more than once to do this. what makes you think that you do?

avatar image Inok gjf · Apr 23, 2016 at 11:30 PM 0
Share
     void OnTriggerEnter2D(Collider2D coll)
        {
           if (coll.gameObject.GetComponent<Tag_Obj>() != null) // 1 call
           {
              var tags = coll.gameObject.GetComponent<Tag_Obj>().Tags; // 2 call
              for (int i = 0; i < tags.Length; i++)
              {
                 if (tags[i] == Tag_Obj.Tag.WHEEL)
                 {
                  // do staff
                 }
              }
           }
        }

2 Replies

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

Answer by gjf · Apr 23, 2016 at 11:59 PM

why not this?

 void OnTriggerEnter2D(Collider2D coll)
 {
     var tag = coll.gameObject.GetComponent<Tag_Obj>();
     if (tag != null)
     {
         var tags = tag.Tags;
         for (int i = 0; i < tags.Length; i++)
         {
             if (tags[i] == Tag_Obj.Tag.WHEEL)
             {
                 // do stuff
              }
         }
     }
 }
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
1

Answer by Owen-Reynolds · Apr 24, 2016 at 02:28 AM

When make the real game -- build for PC, etc... , a Null Reference Exception will crash it.

When you run in the Editor, it keeps running after a NullRefE just to help you test better. But that's just an extra editor-only thing.

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

42 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

Related Questions

NullReferenceException: Object reference not ... ( Assistance please ) 2 Answers

NullReferenceException: Object reference not set to an instance of an object 3 Answers

Custom class instance always return null 1 Answer

null reference help. i fundamentally dont understand. 1 Answer

why does a line renderer cause a nullreference excepetion? 0 Answers


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