Search Unity

Network: can't correctly sync data between objects

Discussion in 'Editor & General Support' started by flogic, Oct 30, 2014.

  1. flogic

    flogic

    Joined:
    May 2, 2014
    Posts:
    5
    I am writing a game where several people play together: first person connecting is a server, others are clients.
    Each is represented by Player class, that has "myScore", which represents score that is shown near the player.

    Each client creates its Player instances with Network.Instantiate(), when connected to server (OnConnectedToServer())

    Say now there are 4 players connected.

    If I understand correctly, every player has 4 instances of the Player class.
    Now I want player 2 to tell everybody that his score is "123". So he has to send an RPC to everybody with the score, and the RPC function should look like:

    [RPC]
    void PlayerScoreUpdate(int score, NetworkMessageInfo info)
    ...

    So player2, in his Update() function calls:
    networkView.RPC("PlayerScoreUpdate", RPCMode.All, myScore);

    But this function is then accepted on all 4 clients for all 4 Player instances, correct?
    And if yes, how can recipient (inside the RPC function) figure out whether he should update myScore (i.e processing Player instance that belongs to player2 on all 4 clients) or ignore it (because he is currently seeing player1,player3 or player4)?
    And when the myScore is assigned to the correct instance, it will also be correctly shown near player2 in all 4 clients.

    Been battling this for some time now.
    Thanks!