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
0
Question by ThiagoTejo · Nov 11, 2013 at 03:53 AM · bugnullreferenceexceptionignorenullreference

Can I Ignore Null Reference Exceptions? ( bug? )

I'm getting an odd problem here, i won't post my code because its giantic and it really doesnt matter, but, i have two codes that are exactly the same logic and all, and what they do, is to turn off certain gameobjects. the first one works great, it turn offs the objects i want, ( using that SetActive thing ), but the other one, it does work exactly like the first one, but every time it desactive a gameobject, it says NullReferenceException, like it was trying to find the object after destroyed it or something... but the script continues to work fine. I don't know if you guys got it, but what's happening is that, both scripts works fine and iddentically, but one of them fill my console with null reference exceptions... i want to know if it is a real problem, or if i can ignore it. I had this problem with other script long ago, and i remember to just restarting unity and everything returned normal, but not this time. Well, that's it... hope you guys can awser me... (sorry for my bad english)

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

Answer by bkachmar · Sep 09, 2017 at 09:34 AM

I had a similar problem and in my case it turned out that I'm disabling the whole gameOblect, but keep reference to the script.

When I switched to keeping references to gameObject this disappeared.

Hope this will help.

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 tormentoarmagedoom · Sep 09, 2017 at 11:04 AM

You should use

 if(GameObject != null)
 {
 }

So if the thing you want to modify does not exist, is never called so no more Null Reference Exceptions

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 Pangamini · Sep 09, 2017 at 03:46 PM

Short answer: Don't ignore it

Unhandled exceptions from MonoBehaviour message calls (such as Update, OnEnable, Start etc) are caught by Unity so your game doesn't crash. However, such exceptions terminate the current call stack (up to the handling point which is before unity call to your code). This means your method call is left unfinished and may lead to unpredictable behaviour. Also, having exceptions logged in the console every update really beats down the performance. Ideally, you should never have exceptions printed in the console (either avoid them or handle them yourself). However, Unity does help a lot by making references to destroyed UnityEngine.Object instances appear to be null (they are not really null, it's just how their operators are overloaded) so all you need to do is check if it's null or not.

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

21 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

Related Questions

Unity null check 1 Answer

iTween nullref error. Hashtable syntax problem? Not sure. 3 Answers

Unexplanable NullReferenceException Error 2 Answers

Why am I getting NullReferenceException on my .Contains()? 1 Answer

Getting error on instatiating. 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