neuecc / UniRx

Reactive Extensions for Unity
MIT License
7.01k stars 895 forks source link

"NullReferenceException" in Observable.EveryLateUpdate when Disabling Domain Reloading #490

Open kyubuns opened 3 years ago

kyubuns commented 3 years ago

When I set Domain Reload to disable in PlayerSettings > Editor > Enter PlayMode Options, the following code throws NullReferenceExteption. After setting Domain Reload to disable, enter PlayMode twice.

NullReferenceException: Object reference not set to an instance of an object
UniRx.MainThreadDispatcher.LateUpdateAsObservable () (at Assets/Plugins/UniRx/Scripts/UnityEngineBridge/MainThreadDispatcher.cs:643)
UniRx.Observable.EveryLateUpdate () (at Assets/Plugins/UniRx/Scripts/UnityEngineBridge/Observable.Unity.cs:717)
Test.Start () (at Assets/Test.cs:8)
using UniRx;
using UnityEngine;

public class Test : MonoBehaviour
{
    public void Start()
    {
        Observable.EveryLateUpdate()
            .Subscribe(x => Debug.Log("a"));
    }
}

Environment

AlexMeesters commented 2 years ago

Right now, what happens is, UniRX is still trying to call LateUpdate on your object. But it doesn't exist anymore. This also happens during runtime if you decide to remove the object in the inspector.

You could make it work by adding AddTo(this), so it unsubscribes when the GameObject has been destroyed:

 Observable.EveryLateUpdate()
            .Subscribe(x => Debug.Log("a"))
            .AddTo(this);