I’m working on a multiplayer lobby menu. Each player selects a role from a checklist, and once a player has checked a box, it becomes grayed out (interactable = false) to the rest of the players. So far so good. The checkboxes are Toggle UI elements attached to a Canvas, and exist in the scene (i.e. not spawned by the network) with a custom NetworkBehaviour to handle synchronization.
My problem is that the server isn’t spawning the checkboxes when I call NetworkServer.SpawnObjects(). Through some testing, I found out the problem is that they are children of the Canvas. Is there a way to get NetworkServer.SpawnObjects() to spawn child game objects with NetworkIdentity’s?
As an alternative, I’ve tried editing the NetworkBehaviour of the checkbox to spawn on startup:
public override void OnStartServer() {
NetworkServer.Spawn(gameObject);
}
However, that code, for whatever reason, isn’t running.
What output are you getting with the logging on Network Manager turned up? There is a bunch of debug logs that will tell you the state of the spawning if you set the Log Level to Debug or higher. “SpawnObjects sceneId:” + uv.sceneId + " name:" + uv.gameObject.name will appear for each spawned object on the server if the first part of the object spawn works. Unity doesn’t like child NetworkBehaviour objects much, it seems to expect NetworkBehaviour objects to be at root level
With Network Manager logging turned up to Developer, it doesn’t list anything about my scene game object when I start the server. The only reference to it I can get is when I click on it in the inspector, and it’ll print (even when I’m not playing): “This is a pure scene object: Checkbox”.
But what if you first spawn the objects without a parent, and right after spawning, set their parent to canvas.
Alright, after a lot of work I got a setup that works: instead of having a network identity directly on the checkboxes, there’s sort of a “checkbox manager” that has no parent and exists in the scene, which I can then spawn. It’s a shame that SpawnObjects() doesn’t work for child objects, but maybe Unity will add that functionality eventually.
The NetworkIdentity should be on the root GameObject as docs says, If you want another behavior, either write in the feedback thread in MultiPlayer forum (sticky thread on top) or at feedback.unity3d.com