Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by VeNN0m · Jun 16, 2017 at 08:09 AM · networkingnetworkphotonteamteams

Photon team assignment

So I was trying to create a photon script that assigns users to a team and then displays them on separate panels once the match is starting. I will show you the code and the results first. The code:

Assign me to a team after I join the room:

 void OnJoinedRoom(){
         Debug.Log(PhotonNetwork.playerList.Length);
         Debug.Log(PhotonNetwork.room.MaxPlayers);
         if(PhotonNetwork.playerList.Length <= PhotonNetwork.room.MaxPlayers/2){
             Debug.Log("Assigned Red");
             PhotonNetwork.player.SetCustomProperties(new Hashtable() {{"team","red"}});
         }else{
             Debug.Log("Assigned Blue");
             PhotonNetwork.player.SetCustomProperties(new Hashtable() {{"team","blue"}});
         }
         joined = true;
     }

When 4 players joined, start the game ( this lies in void Update() ) :

 if(joined && PhotonNetwork.playerList.Length == 4 && !doOnce){
             Debug.Log("Start");
             doOnce=true;
             //this.GetComponent<PhotonView>().RPC ("StartGame", PhotonTargets.All);
             StartGame();
         }

StartGame():

 void StartGame(){
         inRoomPanelGo.SetActive(false);
         champSelectGo.SetActive(true);
         started = true;
     }

Once started is set to true, and players go into champ select this code runs. Also in void Update() :

 if(started){
             foreach(PhotonPlayer pl in PhotonNetwork.playerList){
                 Debug.Log(pl.NickName + " IS HERE " + "AND HE IS TEAM " + pl.customProperties["team"]);
                 if(pl.customProperties["team"]=="blue"){
                     Debug.Log(pl.NickName + "   " + pl.customProperties["team"]);
                     GameObject spawnedCsPlayerBl = Instantiate(csPlayerPrefabBlue, Vector3.zero, Quaternion.identity)
                      as GameObject;
                     spawnedCsPlayerBl.transform.SetParent(blueContainer, false);
                     spawnedCsPlayerBl.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
                 }else{
                     Debug.Log(pl.NickName + "   " + pl.customProperties["team"]);
                     GameObject spawnedCsPlayerRd = Instantiate(csPlayerPrefabRed, Vector3.zero, Quaternion.identity)
                      as GameObject;
                     spawnedCsPlayerRd.transform.SetParent(redContainer, false);
                     spawnedCsPlayerRd.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
                 }
             }
             started=false;
         }

The last piece of code basically checks for all the players in the room and based on their "team" tag it instantiates a Image UI ( with a text that reads their name ) in the correct spot ( blue panel or red panel ). But the results are totally random. The reading is actually successful. Here is the from the debugging in the last function:

DebugLog

IMPORTANT: The order that you see in the debug log is the order i joined the room.

TestUser1 -> TestUser2 -> TestUser3 -> Sphades

The debug log ran on the "Sphade" instance which was ran in the Editor. The other 3 where ran from a built Standalone.

The debug looks fine. The results however...

Note: Left is blue side Right is red side

TestUser1: alt text

TestUser2: alt text

TestUser3: alt text

Sphades: alt text

As you can see, the order in the actual UI is totally random. But they get assigned well in the Debug. And I can't figure out why.

One more thing:

If I run this part of code:

     if(started){
                 foreach(PhotonPlayer pl in PhotonNetwork.playerList){
                     Debug.Log(pl.NickName + " IS HERE " + "AND HE IS TEAM " + pl.customProperties["team"]);
                     if(pl.customProperties["team"]=="blue"){
                         Debug.Log(pl.NickName + "   " + pl.customProperties["team"]);
                         GameObject spawnedCsPlayerBl = Instantiate(csPlayerPrefabBlue, Vector3.zero, Quaternion.identity)
                          as GameObject;
                         spawnedCsPlayerBl.transform.SetParent(blueContainer, false);
                         spawnedCsPlayerBl.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
                     }else if(pl.customProperties["team"]=="red"){
                         Debug.Log(pl.NickName + "   " + pl.customProperties["team"]);
                         GameObject spawnedCsPlayerRd = Instantiate(csPlayerPrefabRed, Vector3.zero, Quaternion.identity)
                          as GameObject;
                         spawnedCsPlayerRd.transform.SetParent(redContainer, false);
                         spawnedCsPlayerRd.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
                     }
                 }
                 started=false;
             }

Instead of:

 if(started){
             foreach(PhotonPlayer pl in PhotonNetwork.playerList){
                 Debug.Log(pl.NickName + " IS HERE " + "AND HE IS TEAM " + pl.customProperties["team"]);
                 if(pl.customProperties["team"]=="blue"){
                     Debug.Log(pl.NickName + "   " + pl.customProperties["team"]);
                     GameObject spawnedCsPlayerBl = Instantiate(csPlayerPrefabBlue, Vector3.zero, Quaternion.identity)
                      as GameObject;
                     spawnedCsPlayerBl.transform.SetParent(blueContainer, false);
                     spawnedCsPlayerBl.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
                 }else{
                     Debug.Log(pl.NickName + "   " + pl.customProperties["team"]);
                     GameObject spawnedCsPlayerRd = Instantiate(csPlayerPrefabRed, Vector3.zero, Quaternion.identity)
                      as GameObject;
                     spawnedCsPlayerRd.transform.SetParent(redContainer, false);
                     spawnedCsPlayerRd.transform.GetChild(0).GetComponent<Text>().text=pl.NickName;
                 }
             }
             started=false;
         }

Then the red part is ignored completly. Als if pl.customProperties["team"] is not equal to "red". Which obviously is as you can see in the debug log.

I can't wrap my head around it. If needed more details, ask. Please help me on this one!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ChristianSimon · Jun 19, 2017 at 08:01 AM

Hi,

it seems that the Player Properties are not updated on every client, when you try to access them. You have the following condition if(pl.customProperties["team"]=="blue") {...} else {...}. If Player Properties are not updated when trying to access them here, the 'else' branch will always be processed and furthermore players are always marked as team 'red' in this case.

You should also try to avoid calling StartGame(); from the Update() function. Another option you have here is to use OnPhotonPlayerConnected(PhotonPlayer newPlayer) callback on the MasterClient. The MasterClient can check the player count whenever this callback is executed and inform the other players to start the game, e.g. by using a RPC or RaiseEvent function. You might want to add some short delay before updating the UI as well to make sure that each client has received the up-to-date Player Properties.

You can also consider about taking a look at PunTeams script which comes with the PUN package from the Asset Store. It basically does the same as you have implemented and also uses the Player Properties for storing the current team.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

151 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Photon WASD working but Virtual Joystick won't work after another player has joined the network...? How can i sync joystick. 2 Answers

Photon camera problum 0 Answers

Unity Photon doesn't Spawn Player 0 Answers

Lerping to Smooth Network Movement 0 Answers

PHOTON FindFriends (222) not allowed on current server (GameServer) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges