Unity Photon Network CustomPropeties not updating after loading new scene

Viewed 891

I'm working on an online multiplayer game on Unity using Photon PUN and I'm having problems with the room CustomProperties.

In the original lobby screen, when I change the room properties it updates for all players, but when I start the game, loading a new level, I want to use CustomProperties so the MasterClient can set the same start time for all players. I'm setting/getting the custom properties from a Game Settings class attached to a DoNotDestroyOnLoad GameObject and the following getter/setter:

private static T GetProperty<T>(string key, T deafult)
{
    if (PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue(key, out object value))
    {
        return (T)value;
    }
    else return deafult;
}

private static void SetProperty(string key, object value)
{
    ExitGames.Client.Photon.Hashtable table = PhotonNetwork.CurrentRoom.CustomProperties;
    if (table.ContainsKey(key))
    {
        table[key] = value;
    }
    else
    {
        table.Add(key, value);
    }
}

When I load the new scene, the previous CustomProperties remain unchanged, and new changes appear locally but do not appear for other players. I have tried using OnRoomPropertiesUpdate() but it doesn't activate after loading the new scene. Is there maybe something else that needs to be attached to a DoNotDestroyOnLoad object?

1 Answers

After changing the table you have to re-assign it via SetCustomProperties otherwise you are just changing the local copy.

private static void SetProperty(string key, object value)
{
    ExitGames.Client.Photon.Hashtable table = PhotonNetwork.CurrentRoom.CustomProperties;
    
    // No need to check Contains
    // This will either Add a new key or overwrite an existing one
    // -> This is more efficient and already behaves exactly the same ;)
    table[key] = value;

    PhotonNetwork.CurrentRoom.SetCustomProperties(table);
}
Related