Ondisable be called in Awake when use LocalPhysicsMode.Physics3D+DontDestroyOnLoad

OnDisable and OnEnable had been called in TestScene’s Awake:
I had two scene GameEntry.scene and TestScene.scene.

public class Entry : MonoBehaviour
{
    public LocalPhysicsMode LocalPhysicsMode = LocalPhysicsMode.Physics3D;

    void Start()
    {
        Debug.Log($"Entry Start  LocalPhysicsMode = {LocalPhysicsMode}");
        SceneManager.LoadScene("TestScene", new LoadSceneParameters(LoadSceneMode.Additive, LocalPhysicsMode));
    }
}
public class TestSceneBehaviour : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log($"test scene awake {Time.frameCount}");
        DontDestroyOnLoad(gameObject);
    }

    private void Start()
    {
        Debug.Log($"test scene start {Time.frameCount}");
    }

    private void OnEnable()
    {
        Debug.Log($"test scene OnEnable {Time.frameCount}");
    }

    private void OnDisable()
    {
        Debug.Log($"test scene OnDisable {Time.frameCount}");
    }

    private void OnDestroy()
    {
        Debug.Log($"test scene OnDestroy {Time.frameCount}");
    }
}

Log:

But when I set LocalPhysicsMode.None, it will be ok:

image

1 Answer

1

Here’s another forum that tackles the issue
https://forum.unity.com/threads/onenable-and-ondisable-getting-called-despite-dontdestroyonload.90584/

It’s likely because DontDestroyOnLoad is taking the object out of the hierarchy and this counts as being disabled, you can could try checking if Application.isLoadingLevel is true when calling enable or disable functions

Thank you for your reply. But when I set the LocalPhysicsMode.None, it will be ok.

I think there are some different code when use different LocalPhysicsMode

Sounds like a Unity bug.