- Home /
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?
Answer by DiegoSLTS · Aug 25, 2016 at 01:23 AM
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
Any info on the proper way to grab references to server spawned objects would be greatly appreciated. I have spent a long time looking through the UNet api and searching forums and I can't figure it out. I also tried writing a custom spawn function according to the example in the documentation, but it wasn't being called for whatever reason. I just don't understand.
FindLocalObject is ok, but will work only after the object is synched. If you make sure the object has been spawned on a client it works. In your question it sounds like you're trying to find the object too soon. Anyway, if you pass the gameObject reference unet will do that for you.
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.
I don't know any method to just get a reference to the local player, but you can search for player objects (with FindObjectsOfType) and see how has isLocalPlayer set to true. Or you can have one reference in some place of easy access to "the local player", and make the local player on each client set te reference itself, then just get that reference on OnStartAuthority from the unit.
If you want all clients to be aware of the player who ownes each unit, maybe you should have a SyncVar on the unit to identify the owner. The server will set that variable and it'll be synched automatically for everyone, no need to search or notify them with another call.
theDude, the answer is simple,
you see in Diego's answer, point 3 ....
"The server instantiates the prefab of the unit and sets any value needed as a SyncVar"
at that point, the server would add the sort of information you mean, which is relevant to your game. for example ... "This item was created by ...", and so on.
Hope it helps!
Wow. Old thread. I think my problem was with the order of callbacks. I was assu$$anonymous$$g the callbacks were being called before they actually were and so my values weren't being initialized when I thought they were.
I have since abandoned any ideas of multiplayer since it's just too complicated for a lone dev like me working in my spare time. I now just focus on single player ideas. I see now that both the HLAPI and LLAPI are being deprecated also. I hope they come up with a better HLAPI in the future.
Your answer

Follow this Question
Related Questions
What is the best method for handling references to objects on Clients from a Network.Spawn? 0 Answers
Multiple world spaces? 0 Answers
Networking | Getting buffered other - previous spawned players 1 Answer
Networking: How to spawn dynamically created objects 2 Answers
Network.Spawn. Not Spawned on clients except for host. 0 Answers