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
public class ScoreManager : MonoBehaviourPun
{
public PhotonView view;
public TextMeshProUGUI player1Text;
List<Player> players = new List<Player>();
void Start()
{
view = GetComponent<PhotonView>();
}
[PunRPC]
public void CreatePlayer(int _index)
{
players.Add(new Player(_index, 0));
Debug.Log("list length" + players.Count);
player1Text.text = players[_index].kills.ToString();
}
[PunRPC]
public void UpdatePlayerKills(int _index)
{
for (int i = 0; i < players.Count; i++)
{
if(players[i].index == _index)
{
players[i].kills += 1;
}
}
}
}
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
How? right now I have a script attached to the player and in the Start() function I call this:
IEnumerator SetMyName()
{
yield return new WaitForSeconds(1f);
if (view.IsMine)
{
int index = PlayerNumberingExtensions.GetPlayerNumber(PhotonNetwork.LocalPlayer);
Debug.Log(index);
scoreManager.view.RPC("CreatePlayer", RpcTarget.All, index);
}
}
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