[SOLVED] Problem with synchronizing variable

Options
Hello. I am making a system that has one variable that is synchronzied on every players so they always have the same value.
public int money;

if (Input.GetKeyDown(KeyCode.F))
{
        PhotonView PV = GetComponent<PhotonView>();
        PV.RPC("IncreaseMoney", RpcTarget.All, 5);
}

[PunRPC]
public void IncreaseMoney(int amount)
{
        money += amount;
}


This is code I wrote, but there's a problem. If there's two players and one player increases the variable, it only increases on the player and does not on the other player.

unknown.png

Player1 is joined in unity editor. When Player2 increases money, it should be increased on every player scripts. However, it only increases on Player2. So as you can see, Player1 can see that Player2 has increased the variable but it's not applied to Player1 itself.

What should I do?

Comments

  • Fixed on my own :smiley: I'll leave the code for people who are having similar problem!

    public int money;

    if (Input.GetKeyDown(KeyCode.F))
    {
    PhotonView PV = GetComponent<PhotonView>();
    PV.RPC("IncreaseMoney", RpcTarget.All, 5);
    }

    [PunRPC]
    public void IncreaseMoney(int amount)
    {
    Player[] players = FindObjectsOfType<Player>();

    foreach (Player item in players)
    {
    item.money += amount;
    }
    }

    // "Player" is the name of my player script. Change it to your own script name.