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 /
avatar image
0
Question by Aerowind · Oct 18, 2016 at 08:00 PM · unity 5bugnullreferenceexceptionnullnullreference

Unity null check

Ok, I'm executing this code:

 _gameData.Initialize();
 var mission = _gameData.Missions.ElementAt(0);
 Debug.Log("Mission is: " + mission.Details.MissionText);
 if (mission == null)
 {
     Debug.Log("Mission is null");
     return;
 }
 Debug.Log("Mission is: " + mission.Details.MissionText);

And I get this output:

alt text

How in the world is that possible? Where's the logic? How am I supposed to make null checks now? And how will null propagation from C#6 work with this?

null.png (42.2 kB)
Comment
Add comment · Show 4
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 Hellium · Oct 18, 2016 at 07:43 PM 1
Share

Are you sure you have provided the whole code ?

avatar image OusedGames · Oct 19, 2016 at 01:33 AM 0
Share

What are you trying to do? Explain better please :)

avatar image JoshuaMcKenzie · Oct 19, 2016 at 05:10 AM 0
Share

I would check your $$anonymous$$ission class (or its base classes) and see if the '==' operator is getting overloaded. also see if it has any implicit type conversions.

I would also recommend not using implicit types (var) until its actually needed even though it'll work. Especially since ElementAt(0) is likely not returning an anonymous type. while this is mostly to help make your code more readable, it may also be that its affecting the equality check (though unlikely).

mission might be implicitly casting to a different type, and casting to a null (typically when using the 'as' keyword)

avatar image NoseKills · Oct 19, 2016 at 07:44 AM 0
Share

Does mission extend UnityEngine.Object?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Thex_PiedDroit · Sep 13, 2017 at 03:34 PM

I think it's probably because it goes there twice (since missions count is 2) and the second one automatically skips the Debug.Log cause it can't access Details.MissionText from a nullobject (though it should make an error i suppose, but i know sometimes it doesn't, and just randomly decides to skip code). Have you tried debugging it with breakpoints to see what exactly is happening?

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

8 People are following this question.

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

Related Questions

Can I Ignore Null Reference Exceptions? ( bug? ) 3 Answers

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

ground detection fails 0 Answers

NullReferenceException question. 2 Answers

Checking if an object is null, results in NRE 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