How do you sync points on UI?

I am trying to sync the points so when a player collides with an object they get a point. The problem is it does not update on the other players screen when that player gets a point. I've been trying to sort this code for a while but can't figure it out how to make it work. Thanks

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Photon.Pun;

public class FlagPoints : MonoBehaviour
{
    public TextMeshProUGUI text;
    public TextMeshProUGUI text2;
    private GameObject Flag;
    PhotonView PV;

    public static int points = 1;
    private int CurrentScore = 1;

    public string tagToCompare = "Floor";

  
    void OnTriggerEnter(Collider col)
    {
        if (col.transform.tag == tagToCompare)
        {
            Debug.Log("It works!");
            if (PhotonNetwork.IsMasterClient)
            {

                
                if (PV.IsMine)
                {
                    PV.RPC("DisplayScore", RpcTarget.All, text);
                }
                Destroy(Flag);
                
            }
            else
            {

                
                if (PV.IsMine)
                {
                    PV.RPC("Display2ndScore", RpcTarget.All, text2);
                }
                Destroy(Flag);

            }
        }
    }

    [PunRPC]
    void DisplayScore(int CurrentScore)
    {
        text.SetText("points:" + CurrentScore++);
    }

    [PunRPC]
    void Display2ndScore(int CurrentScore)
    {
        text2.SetText("points:" + CurrentScore++);
    }



}



Answers

  • Your Rpc method requires an integer parameter , pls check

  • MartinVS
    MartinVS
    edited September 2022

    What you basically want is (EDIT)

    int totalScore = #;
      int Totalscore;
    
     if (PV.IsMine)
                    {
                        PV.RPC("DisplayScore", RpcTarget.All, totalScore);
                    }
      [PunRPC]
    void DisplayScore(int CurrentScore)
    {
         Totalscore +=CurrentScore; //this if you want the score to stack
          text.SetText = "points:" + Totalscore.ToString();
    }