Basic Question about Multiplayer

Hey,

In my game buildings etc. can be enabled and disabled with GameObject.setActive(); , this makes disables and enables selected gameobjects, but when playing in multiplayer, nothing changes for the other players…

Any wait to fix this?

Yes, you can:

The Problem:

GameObject.SetActive() will change the building’s state on your client, but you’re not telling the other players that you are doing this, so they don’t know to do it. This problem will occur with anything like this. In keeping with UNET’s server-authoritative model, what you really should be doing is telling the server what you want to do, and then letting the server tell all the clients (including yourself) what actually happened.

A good analogy is a tabletop rpg: You tell the GM what move you make, do the appropriate rolls, etc., and the GM determines what that roll actually means and what actually happens, and tells everyone.

The Solution:##

Use a Command and RPC:

  1. Parent each interactive building to its own empty gameobject, and then create a networkbehaviour that you add to that empty object.
  2. In that networkbehaviour, get a reference to the child building, and make an RPC that takes a bool, and feeds it to a GameObject.SetActive() call on the child building.
  3. In the networkbehaviour that your player uses to select buildings, write a command that takes in the networkidentity of the target building and the bool value to use. That command should look for an instance of the building networkbehaviour using GetComponent<>(), and if it has one, call the RPC and pass in the bool.
  4. Where your player script would normally call GameObject.SetActive(), call the command instead.