CustomProperties crash?
Options
moofly
✭
Hello, when I call something like this:
I realise I could code it as:
int ai = (int) PhotonNetwork.playerList[index].CustomProperties["avatarIndex"];...I'm getting a crash (well, a NullReferenceException) if the "avatarIndex" property doesn't already exist. Essentially it's returning null and it's crashing trying to convert to a non-nullable type. Is there a better way to do this?
I realise I could code it as:
Object ob =PhotonNetwork.playerList[index].CustomProperties["avatarIndex"]; int ai = (ob == null) ? 0 : (int) ob;But that's quite a mouthful every time I want to access the data. I guess I could also pre-initialise the customProperties too, but I wondered if you were aware of the crash?
0
Comments
-
Hi @moofly,
checking if values are null is the right way here. You can also use TryGetValue function on the CustomProperties Hashtable.object ai; if (PhotonNetwork.playerList[index].CustomProperties.TryGetValue("avatarIndex", out ai)) { // do something }
However you will still have to cast the object to the target type.0