SerializedMonoBehaviour / SerializedScriptableObject nested fields are null

Issue #347 invalid
Ivan Murzak
created an issue

1. What happened?

Class fields are always null, even after force initialize it in the constructor. Every field inside the object of the class Subject<Unit> is null. And the class generate NullReferenceException. If don't use SerializedMonoBehaviour everything is fine.

2. How can we reproduce it?

Add UniRx and just execute this code:

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

public class SerializedClass : SerializedMonoBehaviour
{
    public readonly IObservable<Unit> onDie = new Subject<Unit>();
    protected virtual void Awake()
    {
         ((Subject<Unit>)onDie).OnNext(Unit.Default);
    }
}

4. What version of Unity are you using?

2018.1.1 and 2018.1.4

5. What version of Odin are you using? (See "Tools > Odin Inspector > About")

1.0.6.9

6. Do you have Editor Only mode enabled?

No

7. What operating system are you on?

Windows 10 x64

Comments (2)

  1. Tor Esa Vestergaard

    When using Odin's serialization, you need to understand its serialization behaviour. Taking a look at UniRx's Subject class, I can see that it was not made to be serializable, so everything inside it will break when it is serialized/deserialized - yet you have it as a public field, and public fields are serialized by default, since Odin emulates Unity's serialization behaviour. (See this FAQ answer for more details.)

    As such, this is not a bug. If you wish Odin to leave your public field alone, you must add [NonSerialized] to it, and then your initialization will not be overriden by the (faulty) serialized value.

  2. Log in to comment