Dictionaries not serializing in inspector

Issue #532 resolved
Pat created an issue

I'm relatively new to Odin so I apologize if I'm missing something obvious about the setup. I've gone through "Why is Odin not serializing or showing my field or property?" section and copied the examples from there and am still running into this issue.

1.) a Dictionary<int, string> is not serializing in the inspector. What I see instead is a value of "Null (Dictionary<int, string>)"

2.) - Create a new project - Import Odin - Create the following test script and attach it to a GameObject:

using System.Collections.Generic;
using Sirenix.OdinInspector;

public class OdinTest : SerializedMonoBehaviour
{
    public Dictionary<string, int> test;
}

3.) See attached images.

4.) Reproduced in both Unity 2019.1.0f2 and 2018.3.9f1

5). Odin 2.0.17

6.) No, Editor Only mode is not enabled

7.) macOS 10.14.4

Comments (9)

  1. Pat reporter

    Some additional information: - The Serialization Debugger gives this a green checkmark - There are no errors in the console

    Screen Shot 2019-04-23 at 8.16.00 PM.png

  2. Bjarke Elias

    Hey there, cool that you found the serialization debugger! I'm sure that'll come in handy in the future as well.

    Actually what you see is intended behavior, and something you should get used to with Odin. If something is null, Odin will simply display "Null". Unlike Unity that will automatically create a new instance and assign it for you. But Odin leaves everything as is, and won't automatically do anything with your data.

    If you simply click the object-field. Then you should see a list of all assignable types in a dropdown. From there you should be able to select the "Dictionary<string, int>" type, which should create an instance and assign.

    But if you don't like that it's null by default then you can easily get around that as well, by simply newing it up in the constructor like so:

    public class OdinTest : SerializedMonoBehaviour
    {
        public Dictionary<string, int> test = new Dictionary<string, int>();
    }
    

    And whenever a new "OdinTest" is created, it won't be null by default. You always right-click it and set it to null again if you like.

  3. Bjarke Elias

    Also check out the [HideReferenceObjectPicker] attribute. Odin serialized classes will always show that field, but if you're not interested the "polymophic" features then you can use that attribute to hide it.

  4. Pat reporter

    Thanks a lot for getting back to me, that solved the issue. I really thought I tried selecting both of the options in the object field dropdown but I guess not :( Where can I find documentation on this/related behavior?

  5. Log in to comment