Search Unity

How do you hide a Network Object from host?

Discussion in 'Netcode for GameObjects' started by chochlin, Mar 7, 2023.

  1. chochlin

    chochlin

    Joined:
    Mar 4, 2016
    Posts:
    2
    Hi!

    I'm trying to spawn a network object for a subset of the connected clients. This object should be hidden from the other clients that are connected.

    According to the documentation on object visibility (https://docs-multiplayer.unity3d.com/netcode/current/basics/object-visibility) I should be able to use CheckObjectVisibility to stop the object from being synchronized to the other clients.

    This works great as long as the host is allowed to see the object.
    For situations where the player hosting should not be able to see the object, this does not work. Since the object is instantiated and spawned by the server, the "client-part" of that machine will still see the instantiated object in the scene, regardless of the visibility of the network object.


    Code example of how I am trying to instantiate, control visibility and spawn the object:
    Code (CSharp):
    1. var item = Instantiate(prefab);
    2. var networkObject = item.GetComponent<NetworkObject>();
    3. networkObject.CheckObjectVisibility = id => eligiblePlayers.Contains(id);
    4. networkObject.Spawn();

    Am I approaching this type of selective visibility wrong?

    A workaround I can think of is to instantiate an invisible object, and let each eligible player enable the model and all the scripts and colliders on the clients. This feels like a bad hack to me, so maybe there is a more elegant way of solving this?
     
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @chochlin Netcode for Gameobject is server- authoritative by default, so everything that is networked must be accessible by the server.
    One thing you can do is to either manage the object in a non-networked way and span it through RPCs, or use some logic to hide it on the host client.
     
  3. chochlin

    chochlin

    Joined:
    Mar 4, 2016
    Posts:
    2
    Hi, and thanks for the reply!

    I think I was under the (false) impression the "server" and "client" part of the host was a bit more separated than it seems to be. :)

    Both of your alternatives sounds viable for my situation, so I'll experiment a bit with what works best.
    Thank you again for the suggestions! It was really helpful.
     
    tjumma and RikuTheFuffs-U like this.