com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

OnNetworkSpawn() is not called with specific enter play mode options enabled/disabled.

Open maxsroka opened this issue 3 years ago • 3 comments

Description

OnNetworkSpawn() is not called on NetworkBehaviours under these circumstances:

  1. If Reload Scene and Reload Domain are disabled - OnNetworkSpawn will not be called just for the first time after scripts reload.
  2. If Reload Scene is disabled and Reload Domain is enabled - OnNetworkSpawn will not be called at all.

Reload Scene and Reload Domain are under the enter play mode settings in Projects Settings > Editor.

Reproduce Steps

  1. Add this script to a GameObject with a NetworkObject component:
using UnityEngine;
using Unity.Netcode;

public class Test : NetworkBehaviour
{
    public override void OnNetworkSpawn()
    {
        Debug.Log("OnNetworkSpawn() called");
    }
}
  1. Disable Reload Scene and/or Reload Domain in enter play mode settings.
  2. Enter play mode
  3. Start host

Actual Outcome

The message "OnNetworkSpawn() called" doesn't appear due to OnNetworkSpawn() not being called. If you disabled both Reload Scene and Reload Domain, the message will appear if you enter mode again without reloading scripts. If you disabled just the Reload Scene the message won't appear at all.

Expected Outcome

The message should appear in the console window.

Environment

  • OS: Windows 11
  • Unity Version: 2021.3.0f1
  • Netcode Version: 1.0.0-pre.7

maxsroka avatar Apr 16 '22 18:04 maxsroka

MTT-3457

ashwinimurt avatar May 03 '22 23:05 ashwinimurt

@pixldev I was able to replicate it with the domain reload being enabled and the scene reload being disabled. When you disable the domain reload you can run into a myriad of issues if you exit playmode prior to completely shutting down NetworkManager as is demonstrated in the code example from InitializeOnEnterPlayModeAttribute where they manually reset their static defined integer (s_MySimpleValue). This is because when you exit abruptly with the domain reload disabled, whatever your static properties are set to will be what they are set to when you enter into play mode again.

With that in mind, the default settings for Unity are to have both domain and scene reloading enabled to assure that all of your classes are "reset back to the default state", the AssetDatabase is refreshed/updated, and then it reloads the scene, prior to serializing, in order to assure anything used by the scene is reloaded from the refreshed AssetDatabase.

Netcode for GameObjects v1.0 currently is designed to work with domain and scene reloading enabled (i.e. it works with the default settings). While I can understand that if you have a really large scene it could save development/debugging iteration time by disabling the reloading of the scene, and as such I am going to mark this issue as a feature request.

Before I do this, could you provide additional details as to why you would want either of the two options (domain or scene reloading) disabled and if it is blocking your project? (this will help the team determine the priority of this feature request)

NoelStephensUnity avatar Jul 29 '22 18:07 NoelStephensUnity

Before I do this, could you provide additional details as to why you would want either of the two options (domain or scene reloading) disabled and if it is blocking your project? (this will help the team determine the priority of this feature request)

Having these settings disabled saves a lot of time. It is a nice-to-have, but rather low priority. A warning that these are unsupported could be handy.

maxsroka avatar Jul 31 '22 12:07 maxsroka