How to properly setup my networked game objects

I am making a multiplayer board game. Part of the game is spawning units on the board controlled by a player, and so the player must have authority to issue commands on these non-player objects. I understand that I should use a command function and NetworkServer.SpawnWithClientAuthority to achieve this. In the command, I call a method to initialize a pair of integer SyncVars on the unit’s script. Then I make an RPC call to send the unit gameobject so that the game on the client machine can access this script instance and these integers. However I am getting 0 for both, when they are not 0 on the server. So it seems passing the gameobject from the server command through a RPC does not result in grabbing the correct gameobject on the client’s game.

I tried passing the networkInstanceId instead and using ClientScene.FindLocalObject to grab the script component, but it gives me a null reference exception.

Why is this happening? Is this not the proper way to grab a reference to this type of spawned object?

I got lost in your description, so I’ll tell you how I’d do it:

  • The client calls a Command method from it’s Player object, telling the server whatever it needs to spawn.
  • The server instantiates the prefab of the unit and sets any value needed as a SyncVar
  • The server calls SpawnWithClientAuthority
  • Each client will recieve a message to instantiate a unit with the values of the SyncVar
  • When that unit is created the OnStartClient method of any NetworkBehaviour in that unit will be executed in all clients
  • Since the unit was spawned with client authority, the instance of the client that has authority will also execute the OnStartAuthority method on any NetworkBehaviour in that unit.

You don’t have to tell the clients with an RPC what object was spawned, they’re told by the networking system what to create, and the OnStartClient and OnStartAuthority methods are there for the clients to react with custom code to that event. If you do the Spawn in the server and immediatelly call an RPC with the uint reference, the clients will probably execute the RPC before they had time to process the “spawn” message received.

Check “Object creation flow” and “Spawning Objects with local authority” in the manual for more details: https://docs.unity3d.com/Manual/UNetSpawning.html

Thank you very much for this explanation, but I am still a little confused. I have read the docs many times. It’s just not clicking for me. I see that OnStartClient and OnStartAuthority will be called on the unit, but as far as I can tell they do not give any information about which player object spawned the unit object. This is what I need as it is the player object that will control the movement of all his/her units. The player object has a List of Unit objects. I don’t know the proper way to fill this list. I guess I could loop over all player objects and look for which one has authority and send it the unit reference. I just thought there would be a better way to do it.