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

Request to make NetworkManager.OnSingletonReady public

Open Nuwn opened this issue 2 years ago • 2 comments

I ask if it's possible to turn this internal event into a public, this event already exists in the NetworkManager.

image

It would be nice just to have, for people who wish to subscribe to that event in a non-monobehaviour script, or static scripts, or in RuntimeInitializeOnLoadMethod, etc.

Thank you!

Nuwn avatar Jan 18 '23 07:01 Nuwn

+1

It is so frequent to have components get a NetworkManager.Singleton == null in OnEnable or Start depending on the order the game objects get these calls. That order you can rely on for a loooong time until one point, you make a change to the scene and suddenly everything is broken, primarily the event callbacks.

In the meantime, here's an extension method NetworkManagerExt.InvokeWhenSingletonReady(Action) that hooks into the internal OnSingletonReady via reflection (if the singleton is already initialized, the action is called directly):

https://github.com/CodeSmile-0000011110110111/de.codesmile.netcode/blob/main/Runtime/Extensions/NetworkManagerExt.cs