How do I sync character colors?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How do I sync character colors?

wangf18
2021-05-30 01:06:52

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?

Comments

Tobias
2021-06-21 09:52:38

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.

Back to top