How can I update a property already existing in Photon?
Let’s say inside PlayerLives
I already have a number (let’s say the player has as the moment 2
lives), and in the following function I want to increase it with 1:
public void update_lives()
{
PunHashtable _myCustomProperties = new PunHashtable();
int x = (int)PhotonNetwork.LocalPlayer.CustomProperties["PlayerLives"]; // this is 2
_myCustomProperties.Add("PlayerLives", x++);
PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties);
if (PhotonNetwork.LocalPlayer.CustomProperties.ContainsKey("PlayerLives"))
{
int updated = (int)PhotonNetwork.LocalPlayer.CustomProperties["PlayerLives"];
print("AHOI " + updated); **// this is still 2 as the previous value**
}
}
Why isn’t the value updated?
I also tried setting the custom property to null and then re-assign:
public void update_lives()
{
PunHashtable _myCustomProperties = new PunHashtable();
int x = (int)PhotonNetwork.LocalPlayer.CustomProperties["PlayerLives"];
_myCustomProperties.Add("PlayerLives", null);
_myCustomProperties.Add("PlayerLives", x++);
PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties);
if (PhotonNetwork.LocalPlayer.CustomProperties.ContainsKey("PlayerLives"))
{
int updated = (int)PhotonNetwork.LocalPlayer.CustomProperties["PlayerLives"];
print("AHOI " + updated);
}
}
but in this case I get the error
Exception in callback: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.ArgumentException: An item with the same key has already been added. Key: PlayerLives
How can I update the custom property the right way? Thank you for your time!