Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Client preview of game object

Discussion in 'Netcode for GameObjects' started by Flash-Gordon, Jun 27, 2021.

  1. Flash-Gordon

    Flash-Gordon

    Joined:
    Sep 9, 2013
    Posts:
    6
    I have an RTS kind of prototype where I want to display buildings in a preview on the client. The client should be able to choose the position and only when he clicks the request to the server will be executed where the actual spawning happens.

    I tried to create and destroy the objects locally for the preview but this does not work. It gives the error message: Trying to destroy object 0 but it doesn't seem to exist anymore! while still keeping the object around. According to the documentation on object spawning:
    A client should never call destroy on a networked object itself (this is not supported). To destroy an object with client authority, have the client send an RPC to the server, which allows the server to destroy the object.

    The documentation also mentions to use the visibility system. I wonder if this is really the best way to implement this. To me it seems quite extensive to send a network request for every mouse position just for the server to move a preview object.

    What would be a good pattern to implement this?

    Thanks

    Note: I am not very experienced in unity in general so there might be a more obvious solution that I am missing.
     
  2. danb1

    danb1

    Joined:
    Jun 14, 2019
    Posts:
    6
    You should be able to Instantiate a non-NetworkObject copy of the previewed building on the client with no interaction with the server at all. It sounds like you tried this, but with a gameobject that has an attached NetworkObject component, so just make a copy of your prefab with the component removed. Then, only when the client clicks you send a ServerRpc to have the server Instantiate + Spawn the actual object with the NetworkObject attached and remove the preview version.
     
unityunity