- Home /
Photon Unity Networking Score Issue
I am a begineer photon user. I have a problem by integrating it. The question is I have a prefab "spinner" with child canvas showing score and it has a player script [code downwards]. when other clients instantiated then the player only can see its own score but other client's score shows 0 on every player. Help..!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Player : Photon.MonoBehaviour { Camera playerCam; Text scoretext; // Use this for initialization int score; int id; void Awake() { score = 0; id = PhotonNetwork.player.ID; Debug.Log (id); // DontDestroyOnLoad (gameObject); // //playerCam = GetComponentInChildren (); // // if (!photonView.isMine) { // //// playerCam.gameObject.SetActive (false); // } scoretext = GameObject.Find ("Spinner(Clone)/CanvasSpinner/Panel/Score").GetComponent ();
}
void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.tag == "enemy") {
Debug.Log ("collision");
if (photonView.isMine) {
score += 1;
scoretext.text = score.ToString ();
GetComponent<PhotonView> ().RPC ("UpdateScores", PhotonTargets.All,score);
}
Destroy (col.gameObject);
GetComponent<PhotonView> ().RPC ("UpdateScores", PhotonTargets.All);
}
}
[RPC]
void UpdateScores(int test) {
scoretext.text = test.ToString();
Debug.Log (scoretext.text);
//Keep in mind that it doesn't matter that 'username' and 'score' above are the same as when you send it; they just need to both be the same Type of variable, such as string and float.
//You now have the username and the score on other clients. Do your 'update' stuff here.
}
Answer by ChristianSimon · Jul 10, 2017 at 07:52 AM
Hi,
you can use Photon's built-in score functionality. It stores the score data in the Custom Player Properties and furthermore makes it available automatically on any other client.
You can increase the score by calling PhotonNetwork.player.AddScore(int scoreToAddToCurrent)
and get the value by calling PhotonNetwork.player.GetScore()
in order to update the displaying component / game object.
Your answer
Follow this Question
Related Questions
Photon wont sync for the masterclient. 1 Answer
Switching weapons in PhotonNetwork 1 Answer
Photon Network instantiate problem when Master changes 0 Answers
This is giving an error for the other client and cannot hit other client. 1 Answer
[UNET] Only spawn certain server objects on local client? 0 Answers