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 MunibSyed · Aug 04, 2017 at 09:51 AM · nullcustom editorcustom-inspectornull reference exceptioncustom inspector

Dictionary becomes null unexpectedly when using a custom inspector

So I've been playing around with inspector scripts for the first time and I'm getting an unusual problem that I assume is related somehow to the way that inspector scripts behave:

 [CustomEditor(typeof(TextureSaver))]
 public class TextureSaver : Editor
 {
     private Dictionary<string, string> matPathToTexPath;

 // Use this for initialization
     void Start()
     {
         matPathToTexPath = new Dictionary<string, string>();
     }

     void BuildTextureDatabase()
     {
         if (matPathToTexPath == null)
         {
             Debug.Log("Problem!");
         }
     }

     public override void OnInspectorGUI()
     {
         if (GUILayout.Button("Save Textures"))
         {
             BuildTextureDatabase();
         }
     }
 
 }


The problem is that if I go into play mode and then hit the "Save Textures" button, it prints "Problem" telling me that the dictionary is somehow null. How is this possible when I clearly set it in Start()?

If I create an Update() function and I use a keydown event to trigger BuildTextureDatabase(), then the dictionary is initialized properly. This leads me to believe that the problem is related to the way that editor scripts work.

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 ShadyProductions · Aug 04, 2017 at 09:55 AM 0
Share

OnInspectorGUI is called before start. That's the only possible reason your dictionary is null.

You can always initialize it directly, and not in start.

avatar image MunibSyed ShadyProductions · Aug 04, 2017 at 10:40 AM 0
Share

Yes, but BuildTextureDatabase() is not called until I press a button in the inspector. I do this after I run the game, so BuildTextureDatabase() must be getting called after Start().

2 Replies

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

Answer by ShadyProductions · Aug 04, 2017 at 01:50 PM

So my guess is that Start() is not called because it's not derived from MonoBehaviour but Editor, it seems Editor does not have a Start() or Update().

You will have to initialize it immediately or use lazy if prefered.

 private Dictionary<string, string> matPathToTexPath = new Dictionary<string, string>();

or lazy:

  private Dictionary<string, string> _matPathToTexPath;
  private Dictionary<string, string> matPathToTexPath
  {
      get { return _matPathToTexPath ?? (_matPathToTexPath = new Dictionary<string, string>()); }
  }
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 MunibSyed · Aug 04, 2017 at 10:13 PM 0
Share

Awesome, that fixed it.

avatar image Bunny83 · Aug 04, 2017 at 11:58 PM 0
Share

Right. Note that Editors do have an Awake, OnEnable, OnDisable and OnDestroy callback since they are derived from ScriptableObject. Though when it comes to collection classes like Lists or dictionaries, a field initializer is usually the easiest solution.

avatar image
-1

Answer by efeguclu · Aug 04, 2017 at 11:31 AM

It's null because you have no values in your dictionary.. And if you want values as Texture you should use Dictionary

make sure you add line : using System.Collections.Generic; in first line

Comment
Add comment · Show 4 · 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 MunibSyed · Aug 04, 2017 at 11:34 AM 2
Share

A dictionary can be empty and not null.

avatar image efeguclu MunibSyed · Aug 04, 2017 at 12:04 PM -1
Share

null means empty . so if your dictionary is empty it means that it is null. you do not need to check if your dictionary exist because your code know that it exist.. try changing start void with this and you'll see the change :

      void Start()
      {
          matPathToTexPath = new Dictionary<string, string>() { { "mystring" , "this is my string" } };
      }
avatar image ShadyProductions efeguclu · Aug 04, 2017 at 01:42 PM 1
Share

what you are saying is so wrong, there is a big difference between null and empty. A string for example can be empty (string.Empty) or null. They are both 2 different things.

Show more comments

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

70 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 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

Custom editor window does not show prefab overrides 2 Answers

Getting a target's gameobject in custom editor 1 Answer

Custom inspector drop down 0 Answers

How to Hide/Show List or Array in the inspector based on a variable? 0 Answers

Events in custom inspector 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