Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Third Party [PhotonPUN]Second Player Disappers on First Multiplayer Game

Discussion in 'Multiplayer' started by bekiryanik, Jan 24, 2016.

  1. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    Hello,

    I am working on a real time multiplayer project with Unity 3D. It is 2D game. I change the view of the player by changing it's sprite. But when i change the sprite of the local player and when i join the lobby, and when i connect that lobby from different device, i can't see the change of the other player. So the question is, how can i update the changed view of the player? Or how can i update the other player's change from local device?
     
  2. bekiryanik

    bekiryanik

    Joined:
    Jul 6, 2014
    Posts:
    191
    I solved the problem by changing my script from this:
    Code (csharp):
    1.  
    2. public static int playerViewInt;
    3. public GameObject mainOutline;
    4. public GameObject top;
    5. public Sprite[] outlineSprite;
    6. public Sprite[] topSprite;
    7.  
    8. public void startChangeView()
    9. {
    10. playerViewInt = PlayerPrefs.GetInt("PlayerViewINT");
    11. PhotonView photonView = PhotonView.Get(this);
    12. photonView.RPC("changeView", PhotonTargets.All, null);
    13. changeView();
    14.  
    15. }
    16.  
    17. [RPC]
    18. public void changeView()
    19. {
    20.  
    21. if(playerViewInt == 0)
    22. {
    23. mainOutline.transform.GetComponent().sprite = outlineSprite[0];
    24. top.transform.GetComponent().sprite = topSprite[0];
    25. }
    26. if(playerViewInt == 1)
    27. {
    28. mainOutline.transform.GetComponent().sprite = outlineSprite[1];
    29. top.transform.GetComponent().sprite = topSprite[1];
    30.  
    31. }
    32. if(playerViewInt == 2)
    33. {
    34. mainOutline.transform.GetComponent().sprite = outlineSprite[2];
    35. top.transform.GetComponent().sprite = topSprite[2];
    36. }
    37.  
    38. if(playerViewInt == 3)
    39. {
    40. mainOutline.transform.GetComponent().sprite = outlineSprite[3];
    41. top.transform.GetComponent().sprite = topSprite[3];
    42. }
    43.  
    44. }
    45.  
    TO:

    Code (csharp):
    1.  
    2. public static int playerViewInt;
    3. public GameObject mainOutline;
    4. public GameObject top;
    5. public Sprite[] outlineSprite;
    6. public Sprite[] topSprite;
    7.  
    8. public void startChangeView()
    9. {
    10. playerViewInt = PlayerPrefs.GetInt("PlayerViewINT");
    11.  
    12. changeView();
    13. }
    14.  
    15.  
    16. public void changeView()
    17. {
    18. if (photonView.isMine)
    19. {
    20. if(playerViewInt == 0)
    21. {
    22. photonView.RPC("changeSprite0", PhotonTargets.AllBuffered, null);
    23. PlayerPrefs.SetInt("PlayerViewINT", playerViewInt);
    24. }
    25. if(playerViewInt == 1)
    26. {
    27. photonView.RPC("changeSprite1", PhotonTargets.AllBuffered, null);
    28. PlayerPrefs.SetInt("PlayerViewINT", playerViewInt);
    29. }
    30. if(playerViewInt == 2)
    31. {
    32. photonView.RPC("changeSprite2", PhotonTargets.AllBuffered, null);
    33. PlayerPrefs.SetInt("PlayerViewINT", playerViewInt);
    34. }
    35.  
    36. if(playerViewInt == 3)
    37. {
    38. photonView.RPC("changeSprite3", PhotonTargets.AllBuffered, null);
    39. PlayerPrefs.SetInt("PlayerViewINT", playerViewInt);
    40. }
    41.  
    42. }
    43. else
    44. {
    45. return;
    46. }
    47.  
    48. }
    49.  
    50.  
    51. [RPC] public void changeSprite0()
    52. {
    53. mainOutline.transform.GetComponent().sprite = outlineSprite[0];
    54. top.transform.GetComponent().sprite = topSprite[0];
    55. }
    56. [RPC] public void changeSprite1()
    57. {
    58. mainOutline.transform.GetComponent().sprite = outlineSprite[1];
    59. top.transform.GetComponent().sprite = topSprite[1];
    60. }
    61. [RPC] public void changeSprite2()
    62. {
    63. mainOutline.transform.GetComponent().sprite = outlineSprite[2];
    64. top.transform.GetComponent().sprite = topSprite[2];
    65. }
    66. [RPC] public void changeSprite3()
    67. {
    68. mainOutline.transform.GetComponent().sprite = outlineSprite[3];
    69. top.transform.GetComponent().sprite = topSprite[3];
    70. }
    71.  
    But right now, i have another problem. When i start local and second device for the first time by clicking on start multiplayer button, the player which is in second device, disappears after the players connect to main game screen. I leave the room after that and start an another multiplayer game for both device and after that it seems on the screen. What could be the problem on the first time multiplayer game set?