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. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question [Pun2] Score system

Discussion in 'Multiplayer' started by SonGokuBg, Nov 18, 2022.

  1. SonGokuBg

    SonGokuBg

    Joined:
    Apr 16, 2020
    Posts:
    128
    I am trying to make something like a scoreboard, but I don't know how. I tried to start by adding new players to a list, but the problem is when a new player joins, they don't have the updated version of the list. please help

    Code (CSharp):
    1. public class ScoreManager : MonoBehaviourPun
    2. {
    3.     public PhotonView view;
    4.  
    5.     public TextMeshProUGUI player1Text;
    6.  
    7.     List<Player> players = new List<Player>();
    8.  
    9.     void Start()
    10.     {
    11.         view = GetComponent<PhotonView>();
    12.     }
    13.  
    14.     [PunRPC]
    15.     public void CreatePlayer(int _index)
    16.     {
    17.         players.Add(new Player(_index, 0));
    18.         Debug.Log("list length" + players.Count);
    19.         player1Text.text = players[_index].kills.ToString();
    20.     }
    21.  
    22.     [PunRPC]
    23.     public void UpdatePlayerKills(int _index)
    24.     {
    25.         for (int i = 0; i < players.Count; i++)
    26.         {
    27.             if(players[i].index == _index)
    28.             {
    29.                 players[i].kills += 1;
    30.             }
    31.         }
    32.     }
    33.  
    34. }
     
  2. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    You can instantiate a score item for each prefab, make a script for the score item prefab, and set the stats accordingly
    Your list doesnt update because you probably dont run it in update, or call it in the callback OnPlaterEnteredRoom, doing 1 of those should do the job
     
  3. SonGokuBg

    SonGokuBg

    Joined:
    Apr 16, 2020
    Posts:
    128
    New players somehow should get the actual version of the list when they join the room
     
  4. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    When they join them to the room, add them to the list
     
  5. SonGokuBg

    SonGokuBg

    Joined:
    Apr 16, 2020
    Posts:
    128
    How? right now I have a script attached to the player and in the Start() function I call this:

    Code (CSharp):
    1. IEnumerator SetMyName()
    2.     {
    3.         yield return new WaitForSeconds(1f);
    4.         if (view.IsMine)
    5.         {
    6.             int index = PlayerNumberingExtensions.GetPlayerNumber(PhotonNetwork.LocalPlayer);
    7.             Debug.Log(index);
    8.             scoreManager.view.RPC("CreatePlayer", RpcTarget.All, index);
    9.         }
    10.     }
     
  6. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    Thats just setting their name, here's what you should do
    1. Create a list of players
    2. In the OnJoinedRoom callback, add player to the list
    3. Create a method which displays the list and call it in Update()
     
  7. SonGokuBg

    SonGokuBg

    Joined:
    Apr 16, 2020
    Posts:
    128
    I don't understand the last step
     
  8. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    If you want to instantiate a GO for each player

    Code (CSharp):
    1. for(int i = 0; i < players.Count; i++)
    2. {
    3.        Instantiate(playerItem, content);
    4. }
    Your not really telling me what you are trying to achieve, please make your objective more clear!
     
  9. SonGokuBg

    SonGokuBg

    Joined:
    Apr 16, 2020
    Posts:
    128
    My game is like shooting game, I want to keep track of how many kills each player has
     
  10. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    You'll have to use custom properties for that, whenever player joins a room, set their custom properties of kills to 0, every time they get a kill, add 1 to it