In-scene placed object's OnNetworkSpawn is invoked before Start method (Not disabled)

Hi folks.

I added a new test NetworkObject (with default NetworkObject settings) in the hierarchy to be sure it’s not disabled then I tested the behavior on server and client side and I got same result.

The test objects’s order of methods like this:
Awake => OnEnable => OnNetworkSpawn => Start.

The test object isn’t child object, it is placed on the root of hierarchy. Also the object isn’t in the same scene as NetworkManager. The scene containing the test NetworkObject is loaded with networkManager.SceneManager.LoadScene(“Gameplay”, UnityEngine.SceneManagement.LoadSceneMode.Single);

Do I missing something? Because I looked these documentation pages ( In-scene placed NetworkObjects and NetworkBehaviour spawning and despawning ) and the order is not matched what I have.

Netcode for GameObjects : 2.1.1
Unity : 6000.0.31f1

Did you load this scene with NetworkManager.Singleton.SceneManager ? If it’s the first scene in the build it may behave differently.

Regardless of the execution order, it is best practice to ignore the execution order of Start and OnNetworkSpawn and treat it as undefined. Because at some point you may want to make an in-scene placed network object a prefab or vice versa and just like that, your scripts might break.

Thus perform anything in Start either in Awake (if it can be done this early) or use OnNetworkSpawn. This puts your mind at ease too.

1 Like

Hi @CodeSmile,

Yes as I mentioned above, This isn’t the first sene and it is loaded by “NetworkManager.Singleton.SceneManager”

I’ll keep that suggestion in mind.

Actually, it’s not really a big issue for me, but I wanted to make sure whether I did wrong or right. Because this behavior doesn’t match on documentation.

That’s true, I also find it odd, but perhaps the docs just haven’t been updated and the behaviour may have change, perhaps for v2.0? Just a guess.

It would need further confirmation from others or a minimal test case in a new project.

1 Like

I’ve come across the same thing and I did mention it here. I’ve not looked into it but just noticed in passing that in some test projects the order is correct and sometimes not. I’ll see about raising an issue for it to see if we can get some clarification.

1 Like

Thank you for pointing this out.
It would appear Unity 6’s SceneManager handles the instantiation process more like it would if you were to dynamically create a network prefab instance and spawn it.

We will start tracking this and update the NGO v2 documentation to reflect this change. I replicated what you are seeing and indeed it does appear that both dynamically an in-scene placed NetworkObjects will be following the same order of operations:

  • Awake
  • OnNetworkPreSpawn
  • OnNetworkSpawn
  • OnNetworkPostSpawn
  • Start

However, for a scene that is already preloaded with in-scene placed NetworkObjects prior to starting the NetworkManager it will still follow the same pattern:

  • Awake
  • Start
  • OnNetworkPreSpawn
  • OnNetworkSpawn
  • OnNetworkPostSpawn
2 Likes

The Pre/Post spawn are new to me. I looked them up with a hunch and indeed, the example use case spells it out:

This can be used to handle things like initializing a NetworkVariable.

This is good to know! :slight_smile:

I’m seeing this happen in Unity 6 but not consistently. In a project I have a single scene where the order is incorrect, if I reproduce it identically in a new scene the order is correct. If I make a file system duplicate of the first files the order remains incorrect. If I copy/paste the game objects into a new scene the order is correct but is incorrect after a re-import.

I hope you can make more sense out of this than I can. :smiley: