How do I sync character colors?

Options
I'm trying to have a system where a player joins a room, picks a color, and their character is instantiated as that color.
//
public void OnPlayerButtonClicked()
    {
        string oldCharacterName = (string)PhotonNetwork.LocalPlayer.CustomProperties["character"];

        if (oldCharacterName != null)
        {
            GetComponent<PhotonView>().RPC("ActivateButton", RpcTarget.AllBuffered, oldCharacterName);
        }
        else
        {
            MakeNicknameUnique();
            spawner.InstantiatePlayer();
            playerModel = spawner.GetPlayer();
            playerModel.GetComponent<PlayerControl>().SetID(PhotonNetwork.LocalPlayer.ActorNumber);
        }

        string newCharacterName = EventSystem.current.currentSelectedGameObject.name;
        GetComponent<PhotonView>().RPC("DisableButton", RpcTarget.AllBuffered, newCharacterName);

        SetCustomCharacter(newCharacterName);

        transform.gameObject.SetActive(false);

        GetComponent<PhotonView>().RPC("SetColor", RpcTarget.AllBuffered);
    }

I set the color as a Player Custom Property in SetCustomCharacter(), and then I'm trying to get a way to link the player prefab to the Photon player. Right now, I'm trying to set the color like this:
//
foreach (GameObject playerModel in players)
        {
            foreach (Player player in PhotonNetwork.PlayerList)
            {
                Debug.Log(playerModel.GetComponent<PlayerControl>().GetPlayerID() + ", " + player.ActorNumber);
                if (playerModel.GetComponent<PlayerControl>().GetPlayerID() == player.ActorNumber)
                {
                    string color = (string) player.CustomProperties["character"];
                    playerModel.GetComponent<Renderer>().material = Resources.Load<Material>("Materials/" + color);
                    
                }
            }
        }

The playerModel.GetComponent<> ... .ActorNumber doesn't work because it's only locally setting the ownerID, and for everyone else it returns 0. What is the correct way to do something like this?

Answers

  • Tobias
    Options
    There are plenty of ways to get this working.
    I would recommend one of those two:
    a) If the players select the color before instantiating the character (and the color never changes afterwards), you can send some "Instantiation Data" with the Instantiate. A script on the prefab should read this and apply the color.
    b) Store the color as a Custom Property of each player. Remote clients can read each player's color property and apply it.