• 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
2
Question by tolmasky · Feb 06, 2014 at 10:42 PM · nullproperty fields

Property not set in inspector, prints null but doesn't == null

I have a property in a MonoBehavior, defined in the following way:

public GameObject whatever = null;

I set it to an actual GameObject reference on some of them, leave it blank in others. Basically, its an optional property. Now, the craziest thing happens when I test it in the program:

Debug.Log(whatever + " " + (whatever == null));

This prints: "null False". I have no idea how to proceed. All I want is to be able to null test it in code.

The following Test Behavior exhibits this issue and requires no further code:

 using UnityEngine;
 using System.Collections;
 
 public class TestBehavior : MonoBehaviour
 {
     public GameObject whatever;
 
     // Use this for initialization
     void Start ()
     {
        Print(whatever);
     }
     
     public void Print<T>(T anObject)
     {
        Debug.Log(anObject + " " + (anObject == null));
     }
 }
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 Maui-M · Feb 06, 2014 at 10:53 PM 0
Share

Do you have this line anywhere? whatever = new GameObject();

Also, posting some more code would help.

This guy ran into the same issue it looks like, try some of the solutions there.

avatar image tolmasky · Feb 06, 2014 at 10:59 PM 0
Share

I do not have that line anywhere. I have edited the question with a full reduction of the problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tolmasky · Feb 06, 2014 at 10:48 PM

Yes I tried that, didn't make a difference.

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 AlkisFortuneFish · Feb 06, 2014 at 11:51 PM

It looks like you've fallen victim to this. Pay specific attention to the second and third answers and kind of ignore the first.

For what you've got at the moment, EqualityComparer from System.Collections.Generic would do the trick.

EqualityComparer.Default.Equals(anObject, default(T))

It is worth examining whether your use case really warrants the use of unconstrained generics, though. The problem in this case is that the compiler cannot know whether T is nullable or not, it could well have been a value type.

I don't know if it would fit your specific use case, but constraining it to types derived from UnityEngine.Object might or might not be enough.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

Animation Error "Null reference exception"? 2 Answers

c# 2D array error: need Help! 1 Answer

Resources.Load texture problem 1 Answer

Fresh installed , with problems 0 Answers

Passing information error 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