Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by paulyfam · Sep 16, 2021 at 12:23 AM · listrandomphotonmultiplayer-networkingonline

Trying to get Photon.PlayerProperties to return not null

Hi there, I'm trying to get the player properties of a local character to return as a random string from a pre shuffled list, but it only returns null. How do I fix this?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Photon.Pun;
 using Photon.Realtime;
 public class PropertyAssigner : MonoBehaviour
 {
     public SpriteRenderer Donkey = new SpriteRenderer();
     public SpriteRenderer Elephant = new SpriteRenderer();
     public SpriteRenderer Porcupine = new SpriteRenderer();
     public SpriteRenderer Beaver = new SpriteRenderer();
     public SpriteRenderer Eagle = new SpriteRenderer();
     public SpriteRenderer Bird = new SpriteRenderer();
     public SpriteRenderer Dinosaur = new SpriteRenderer();
     public SpriteRenderer Rhino = new SpriteRenderer();
 
     public Sprite DonkeySp;
     public Sprite ElephantSp;
     public Sprite PorcupineSp;
     public Sprite BeaverSp;
     public Sprite EagleSp;
     public Sprite BirdSp;
     public Sprite DinosaurSp;
     public Sprite RhinoSp;
 
     List<string> icons = new List<string>();
     List<string> shuffledIcons = new List<string>();
     bool b;
 
     ExitGames.Client.Photon.Hashtable playerProperties = new ExitGames.Client.Photon.Hashtable();
 
 
     // Start is called before the first frame update
     void Start()
     {
 
 
         var hash = new Hashtable();
         icons.Add("Donkey");
         icons.Add("Elephant");
         icons.Add("Porcupine");
         icons.Add("Beaver");
         icons.Add("Eagle");
         icons.Add("Bird");
         icons.Add("Dinosaur");
         icons.Add("Rhino");
 
     }
 
         // Update is called once per frame
         void Update()
         {
             
             
 
             if (b == false)
             {
                 for (int i = 0; i < icons.Count; i++)
                 {
                     Debug.Log(icons.Count);
                     string temp = icons[i];
                     int randomIndex = Random.Range(i, icons.Count);
                     icons[i] = icons[randomIndex];
                     icons[randomIndex] = temp;
                 }
             string s = icons[1];
             playerProperties.Add("icon", s);
             playerProperties["icon"] = s;
             Debug.Log(playerProperties.ToStringFull());
             PhotonNetwork.LocalPlayer.SetCustomProperties(playerProperties);
                 icons.Remove(icons[1].ToString());
                 Debug.Log(PhotonNetwork.LocalPlayer.CustomProperties["icon"]);
                 Debug.Log(icons.ToStringFull<string>());
                 b = true;
             Debug.Log(PhotonNetwork.PlayerList.Length);
             Debug.Log(PhotonNetwork.LocalPlayer.CustomProperties["icon"]);
 
         }
          
         foreach (var item in PhotonNetwork.PlayerList)
             
                 if (PhotonNetwork.LocalPlayer.CustomProperties["icon"] == "Donkey")
                 {
                     Donkey.sprite = DonkeySp;
                 Debug.Log("Donkey Sprite");
 
                 }
 
                 else if (PhotonNetwork.LocalPlayer.CustomProperties["icon"] == "Elephant")
                 {
                     Elephant.sprite = ElephantSp;
                 Debug.Log("Elephant Sprite");
             }
 
                 else if (PhotonNetwork.LocalPlayer.CustomProperties["icon"] == "Porcupine")
                 {
                     Porcupine.sprite = PorcupineSp;
                 Debug.Log("Porcupine Sprite");
             }
 
                 else if (PhotonNetwork.LocalPlayer.CustomProperties["icon"] == "Beaver")
                 {
                     Beaver.sprite = BeaverSp;
                 Debug.Log("Beaver Sprite");
             }
 
                 else if (PhotonNetwork.LocalPlayer.CustomProperties["icon"] == "Eagle")
                 {
                     Eagle.sprite = EagleSp;
                 Debug.Log("Eagle Sprite");
             }
 
                 else if (PhotonNetwork.LocalPlayer.CustomProperties["icon"] == "Bird")
                 {
                     Bird.sprite = BirdSp;
                 Debug.Log("Bird Sprite");
             }
 
                 else if (PhotonNetwork.LocalPlayer.CustomProperties["icon"] == "Dinosaur")
                 {
                     Dinosaur.sprite = DinosaurSp;
                 Debug.Log("Dinosaur Sprite");
             }
 
                 else if (PhotonNetwork.LocalPlayer.CustomProperties["icon"] == "Rhino")
                 {
                     Rhino.sprite = RhinoSp;
                 Debug.Log("Rhino Sprite");
             }
 
             }
 
 
 
             void SetCustomProperties(PhotonPlayer player, string icon)
             {
                 ExitGames.Client.Photon.Hashtable customProperties = new ExitGames.Client.Photon.Hashtable() { { "icon", icon } };
                 player.SetCustomProperties(customProperties);
             }
         }
     
 
 

and if you find any spaghetti code that should be fixed don't hesitate to tell me! Thanks in advance!

Comment
Add comment · Show 4
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
avatar image Captain_Pineapple · Sep 16, 2021 at 11:45 AM 0
Share

your code is confusing as hell. What is this loop for: foreach (var item in PhotonNetwork.PlayerList) ? what does the condition b in Update do?

avatar image paulyfam Captain_Pineapple · Sep 17, 2021 at 12:47 AM 0
Share
 foreach (var item in PhotonNetwork.PlayerList)    

loops through each player in the list and sets an image to their assigned animal. technically I could get rid of the b loop now that I think about it

avatar image Captain_Pineapple paulyfam · Sep 17, 2021 at 07:36 AM 0
Share

please check your code if that is really what's going on. You loop over var item but you never actually access item anywhere.


please update the question if something should change.


In addition as a sidenote: Anything network related should never be done in Update. (e.g. SetCustomProperties or accessing CustomProperties) You cannot afford that these things get triggered 100 times per second. Only do that once when it has to be done.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

144 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

Related Questions

How to instantiate prefabs list in turn photon 1 Answer

How many current players will I get daily if I have 10k installs 1 Answer

A node in a childnode? 1 Answer

How do I Photon Instantiate a scene object randomly from a list of objects? 0 Answers

Pick a random number, then synchronise it? (Unity Photon) 1 Answer


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