Photon Unity Network. Accessing the same variable from different players.

Hi. I am new to developing multiplayer games. I am using photon unity networking to develop a kind of 4 player chess game. When a player joins a room I want them to select a specific color for their pieces. But I want to avoid two players from having the same color. I have saved the color value in a string. And when a player joins the room I want them to access the string value for all other players in the room and compare it with their selection in order to stop them from choosing the same color as any other player in the room. Is there any way to access variables from other players in such a way. If not then can you guys suggest any other method to do that. Thankyou.

You could use Custom Properties and store each player’s selected color in those.
When you go through the list of players, you can get each player’s color and flag that as being in use.
See “Custom Properties”.

1 Like

Alright I’ll try doing that. But I am trying to use a different approach can you please tell me what I’m doing wrong in it. I am instantiating an empty game object for each player when they join the room using PhotonNetwork.Instantiate. And I name the object the same as the player name. I have a script attached to that object which contains the variable in which the color value is saved.
Now I am using the following code to retrieve the color value when a player selects the color. Please tell me if I am doing something wrong. Or will this approach not work at all.

foreach(PhotonPlayer pl in PhotonNetwork.playerList)
{
    if(GameObject.Find(pl.name).GetComponent<colorValueScript>().colorValue==color)
       {

                      //Color Selection Code Here
         }
  }

I have tried debugging it but the GetComponent is not returning any value. Even though the objects are being instantiated in the scene and the colorValue string is showing the right value in the inspector window.
Does the GetComponent not work with the objects instantiated with PhotonNetwork.Instantiate? Or is there any other problem. Thankyou, I would really appreciate the help.

Another question I have is that I was researching the Custom Properties and I read on a forum that the custom properties are only synced as soon as the player joins the room and do not sync after that. Is this true?
In my game the players select the color after joining the room. So if that is true then the Custom Properties will do no good for me. I am sorry if my questions are a bit noobish. But I am new to developing multiplayer and am having a hard time figuring things out. Thanks for the help.

I actually don’t know what’s going wrong here. If the component is there it should also have a colorValue. Maybe it’s a timing issue? Apply a default value to colorValue and check if it’s still the same when you try to figure out the color?

The methods offered by Unity also work on object instantiated with PUN. In the end, PUN of course just wraps some of Unity’s API to initialize our objects as needed.

Custom Properties are of course synchronized when they change. There is even a callback for that.
I assume the post was trying to say that the Custom Properties are available in their current state when you join. Updates are sent when they happen.

Yes you are right. It was a timing issue. After working a little on it I fixed the timing issue but I realized that every object instantiated in each player’s scene has the same colorValue as the player. So it was not helpful in my case. I guess the Instantiate method just instantiates all the objects in each scene and the components are assigned according to the conditions that satisfy that specific player. So this approach does not work in my scenario.

But I was able to do it using the custom properties. Thanks a lot for the help/

1 Like