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 PrimeDerektive · Aug 20, 2014 at 01:11 PM · booleaninventorynullcustom class

Public variable instance of custom class is never null

Is this by design? Here's an example:

 public class NullTest extends MonoBehaviour{
 
     var foo : Bar = null;
 
     function Update(){
         print(foo);
     }
 
     public class Bar{
         var parameter : int;
     }
 
 }

in this script print(foo) will print NullTest+Bar (and evaluate true if used as a boolean) with nothing assigned to its parameters in the inspector... but will evaluate Null if foo is made a private variable.

I'm trying to design an NGUI based inventory system, and in it each inventory slot is a separate sprite object with an InventorySlot script; and I want the top level panel of the GUI to have an InventoryManager that manages all the slots; I ran into this issue because InventoryManager needed to check which slots were "occupied" (if slot.item != null), but because they were all public they never were!

Is adding a "filled" boolean parameter to my InventorySlot script and making the item private pretty much my only option?

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

2 Replies

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

Answer by Landern · Aug 20, 2014 at 01:32 PM

This is documentated, the editor is instantiating the public field:

No support for null for custom classes

pop quiz. How many allocations are made when deserializing a MonoBehaviour that uses this script:

 class Test : MonoBehaviour {
     public Trouble t; 
 }
     
 [Serializable]
 class Trouble {
     public Trouble t1;    
     public Trouble t2;
     public Trouble t3; 
 } 

It wouldn’t be strange to expect 1 allocation. That of the Test object. It also wouldn’t be strange to expect 2 allocations. One for the Test Object, one for a Trouble object. The correct answer is 729. The serializer does not support null. If it serializes an object, and a field is null, we just instantiate a new object of that type, and serialize that. Obviously this could lead to infinite cycles, so we have a relatively magical depth limit of 7 levels. At that point we just stop serializing fields that have types of custom classes/structs and lists and arrays.

Since so many of our subsystems build ontop of the serialization system, this unexpectedly big serializationstream for the Test monobehaviour will cause all these subsystems to perform more slowly than necissery. When we investigate performance problems in customer projects, almost always do we find this problem. We added a warning for this situation in Unity 4.5.

Comment
Add comment · Show 1 · 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 PrimeDerektive · Aug 20, 2014 at 02:01 PM 0
Share

Embarassing. And how many times have I read the docs? Thanks!

avatar image
1

Answer by Brainversation01 · Jun 04, 2015 at 08:11 AM

For anyone looking for a workaround.

 public class NullTest : MonoBehaviour{ 
      private Bar _foo = null;
 
      public Bar foo{
           get{
                return _foo;
           }
           set{
                _foo = value;
           }
      }
  
      function Update(){
          print(foo);
      }
  
      public class Bar{
          int parameter;
      }
  
  }
Comment
Add comment · Show 2 · 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 dCalle · Dec 26, 2016 at 01:51 PM 0
Share

thx, is there a way to make it visible in the inspector?

avatar image Brainversation01 dCalle · Jan 03, 2017 at 04:16 AM 0
Share

Not that I know of sorry.

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

24 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

Related Questions

Confirming item pickup in inventory 0 Answers

Null reference exception 1 Answer

How do I fix this null problem? 1 Answer

List<> and Crafting null reference. 1 Answer

Problem with a Bool 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