0
\$\begingroup\$

I have overridden OnServerAddPlayer and put some custom logic in place to set the rendered prefabs color. Something like this:

    var playerCount = NetworkServer.connections.Count;

    if(playerCount == 1)
    {     
        player.GetComponentInChildren<Renderer>().material.color = Color.green;
    }  

When I test the code with two application instances, the server host shows the expect result (first character is green, second is red) but the client instance shows both characters as red (the default).

Is my logic for setting the colour incorrect? or do I need to do something special to ensure that the my clients will see the colour I have set for the characters on the server?

\$\endgroup\$
1
\$\begingroup\$

Firstly, OnServerAddPlayer only runs on a server, so the clients don't run this code. Secondly, combined with the fact that the properties of child objects are not transmitted with a NetworkBehaviour, so the change to set the material color will not be sent to clients.

So you need a client-side hook to leverage when an item is added to the scene; you should implement NetworkBehaviour.OnStartClient within your player class.

https://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.OnStartClient.html

Also look into using the SyncVar attribute; it's used for persisting field values on a class which inherits from NetworkBehaviour.

https://docs.unity3d.com/ScriptReference/Networking.SyncVarAttribute.html

With these combined, you could do something like the below:

public class Player : NetworkBehaviour
{
    [SyncVar] private Color objectColor;

    public virtual void OnStartClient(NetworkClient client)
    {
        this.GetComponentInChildren<Renderer>().material.color = objectColor;
    }
}

FYI: A good overview of these concepts is here: Unity Documents - Object Spawning

\$\endgroup\$
2
  • \$\begingroup\$ Thanks Neil, this was the bit which caught me out " the properties of child objects are not transmitted with a NetworkBehaviour," I've managed to get round this by registering a second prefab in the network manager and spawning a different instance rather than changing the colour. The prefab seems to be transmitted in it's entirety to the client, so making a decision about which to spawn made more sense. The syncvar info is very useful for me going forward though to understand how to best manage the client server comms and game state syncronisation. \$\endgroup\$ Sep 13 '16 at 13:30
  • 1
    \$\begingroup\$ Glad I could help. I suspect the component properties aren't sent to clients to reduce the overall network traffic. A different prefab is a suitable alternative, and will make it self even more useful when you decide you want to modify the details of the player for any other properties. \$\endgroup\$
    – Neil Cross
    Sep 14 '16 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.