- Home /
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!
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?
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
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.
Your answer
Follow this Question
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