public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
Debug.Log("OnNetworkSpawn Called");
if (IsClient)
{
Debug.Log("I'm the client");
TransmitClientScreenSizeToServerRpc(Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height/2f, 0)).x, Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2f, 0, 0)).y);
}
}
which I placed into a gameobjct attached into a scene. The problem is that the OnNetworkSpawn is never called (in Client or the Host).
Here is what I do to test it:
Hit the play button
Start the game as Host
Hit the play button on the second editor (using parallel sync)
Creating In-Scene Placed Network Prefab Instances#
In order to create a Network Prefab that can be used as an in-scene placed NetworkObject you must do the following:
In the scene you wish to create the instance (or any open scene) create an empty GameObject and add a NetworkObject component to it.
Add any other NetworkBehaviours required by your in-scene placed NetworkObject.
Drag and drop the newly created GameObject into your Prefab (or associated) folder.
Delete the GameObject instance in your scene (this is required to get a proper GlobalObjectIdHash value assigned)
Finally, drag and drop an instance of your newly created Network Prefab into the scene you wish to have an instance of your in-scene placed NetworkObject.
It doesn’t say in the documetation that yoou need to add it to the networkprefabs list on the Network Manager.
Anyway, didn’t log you an error before you added the object to the networkprefabs list on the Network Manager?
This is not entirely true. I didn’t have to create a prefab and then deleted it from the scene, I simply created a gameobject on the scene and then drag it into the Networkprefab / NetworkObjcts list directly from the scene.
From what I understood you need to create a prefab and then add it into the NetworkObjcts list only if you need to spawn the prefab from code, but id the Gombject is right in the scene from the beginning you can simply leave it there without the need to create a prefab.
Anyway, this topic it’s not explained in the documentation
I think the proper way of doing this is making the scene object a prefab and not adding it to the networkPrefab list, but the way you did it also works
Are you using the Editor Play mode options by any chance? I think there might be a bug where netcode requires a full domain reload in order to call every NetworkBehavior’s OnNetworkSpawn for in-scene placed objects. I’ll keep testing on my end but in the meantime :
You can call start the server, exit the game and then restart it as a server again. A bit hacky I admit, but it works.
Disabling Enter Play Mode Options made the calls work again consistently.