Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Third Party Photon-Having issue with player name and sprite

Discussion in 'Multiplayer' started by IchlasulA, Oct 28, 2020.

  1. IchlasulA

    IchlasulA

    Joined:
    Apr 8, 2020
    Posts:
    4
    hello, i'm having trouble while making multiplayer game with photon, the issue is each client will have the same nickname and sprite,
    • ie : player 1 will have seen player 2 with player 1 name, so there are two player 1 and vice versa
    below is the code i use to take the nickname and the sprite, do keep in mind that i'm relatively new in making games

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine;
    5. using Photon.Pun;
    6. using UnityEngine.SceneManagement;
    7.  
    8.  
    9. public class MainChar : MonoBehaviourPun
    10. {
    11.     public Sprite Female, FemaleCasual, MaleCasual;
    12.     public GameObject nickname;
    13.     private SpriteRenderer mySprite;
    14.     private readonly string selectedCharacter = "selectedCharacter";
    15.     private readonly string selectedChar = "selectedChar";
    16.     public PhotonView PV;
    17.     public static MainChar MC;
    18.     public int getCharacter;
    19.     public int health = 5;
    20.     public int experience = 40;
    21.     public int gold = 1000;
    22.  
    23.     public Quest quest;
    24.     public string sceneName;
    25.  
    26.     void Awake()
    27.     {
    28.         APIHandler.Instance.databaseAPI.GetLocation();
    29.         mySprite = this.GetComponent<SpriteRenderer>();
    30.         PV = GetComponent<PhotonView>();
    31.     }
    32.     void Start()
    33.     {
    34.         if (PV.IsMine)
    35.         {
    36.             PV.RPC("charSelect", RpcTarget.AllBuffered,null);
    37.         /*    charSelect();*/
    38.         }
    39.         sceneName = SceneManager.GetActiveScene().name;
    40.         APIHandler.Instance.databaseAPI.PostLocation(sceneName);
    41.     }
    42.  
    43.     [PunRPC]
    44.     void charSelect()
    45.     {
    46.         nickname.GetComponent<TextMeshPro>().text = APIHandler.Instance.authAPI.GetUser().nickname;
    47.        
    48.         print(nickname.GetComponent<TextMeshPro>().text = APIHandler.Instance.authAPI.GetUser().nickname);
    49.        
    50.         string noChar = APIHandler.Instance.authAPI.GetUser().noCharacter;
    51.         int getCharacter;
    52.         getCharacter = int.Parse(noChar);
    53.  
    54.         print(getCharacter);
    55.  
    56.         switch (getCharacter)
    57.         {
    58.             case 1:
    59.                 mySprite.sprite = Female;
    60.                 break;
    61.             case 2:
    62.                 mySprite.sprite = MaleCasual;
    63.                 break;
    64.             case 3:
    65.                 mySprite.sprite = FemaleCasual;
    66.                 break;
    67.             default:
    68.                 mySprite.sprite = FemaleCasual;
    69.                 break;
    70.         }
    71.     }
    72.  
    73.     public void goBattle()
    74.     {
    75.         health -= 1;
    76.         experience += 2;
    77.         gold += 5;
    78.  
    79.         if (quest.isActive)
    80.         {
    81.             quest.goal.EnemyKilled();
    82.             if (quest.goal.IsReached())
    83.             {
    84.                 experience += quest.expReward;
    85.                 gold += quest.goldReward;
    86.                 quest.Complete();
    87.             }
    88.         }
    89.     }
    90. }
    91.  
     
    BlueDia likes this.
  2. kopf

    kopf

    Joined:
    Sep 1, 2017
    Posts:
    11
    First, maybe consider using the RPC from inside Awake(), and after you set your PV variable.

    Secondly, I think your problem happens because when you RpcTarget.All.

    This is calling the RPC on that object to everyone in the room, which leads me to believe when you do APIHandler.Instance.authAPI.GetUser().nickname, you are actually getting the nickname of the LocalPlayer. Doing this would lead to the unintended result of every MainChar object in that room being labelled with the same LocalPlayer's nickname.
     
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Your CharSelect RPC function should take the nickname as a parameter the value of which should be determined before sending the RPC.
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    The PUN Basics Tutorial shows how to use the nickname and show it per player.
    I would recommend reading it and coding along.
     
  5. IchlasulA

    IchlasulA

    Joined:
    Apr 8, 2020
    Posts:
    4
    i see, i'm gonna try your suggestion thanks for helping
     
  6. IchlasulA

    IchlasulA

    Joined:
    Apr 8, 2020
    Posts:
    4
    hmmm ok i'll try that too, thanks for helpng
     
  7. IchlasulA

    IchlasulA

    Joined:
    Apr 8, 2020
    Posts:
    4
    alright i might do that if i'm still can get the the issue solved, thanks for helping
     
    tobiass likes this.