Question by
seifmahmoudsmay · Jul 05, 2021 at 08:31 PM ·
unity 2dphoton
My score board dosent work at all
Now I am trying to make a score board system. Everytime after a specific match time score system shoud appear now in the score board it dosent show anything (Only 0) I am using Photon Pun2
These scripts are conneted to each other
PlayerScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using Photon.Pun;
using TMPro;
using Photon.Realtime;
using Hashtable = ExitGames.Client.Photon.Hashtable;
public class PlayerMulti : MonoBehaviourPunCallbacks
{
public GameObject h;
public GameObject a;
public GameObject gun1;
public GameObject gun2;
public GameObject gunholder2, gunHolder3, gunHolder4, rifleHolder1, rifleHolder2, rifleHolder3, rifleHolder4, shotGun1, shotGun2, shotGun3, shotGun4, knifeHolder1, knifeHolder2, knifeHolder3, knifeHolder4;
public static PlayerMulti playerMulti;
public GameObject arrowL, arrowR, joyStcik;
public GameObject Allcanvas;
public ShootButton shootButton;
public float runSpeed = 5f;
public Rigidbody2D rb;
public float horizontalMove = 0f;
public JoyButton joy;
public LayerMask whatIsGround;
private float dirX;
public bool onGround;
public Transform groundCheck;
private Vector3 input;
[SerializeField] GameObject ui;
[SerializeField] Item[] items;
int itemIndex;
int previousItemIndex = -1;
// public GameObject scoreBoard, scoreManager;
bool jump;
PhotonView PV;
// bool croush = false;
public Joystick joystick;
private float lastYposition;
private float timeBtwShots;
public float startTimeBtwShots;
private void Awake()
{
playerMulti = this;
PV = GetComponent<PhotonView>();
shootButton = GameObject.Find("Shoot Joystick ").GetComponent<ShootButton>();
if (gun1.activeSelf == true || shotGun1.activeSelf == true)
{
startTimeBtwShots = 1;
}
if (rifleHolder1.activeSelf == true)
{
startTimeBtwShots = 0;
}
}
void Start()
{
//matchLenghtTimeS = 20;
rb = GetComponent<Rigidbody2D>();
lastYposition = transform.position.y;
joystick = GameObject.Find("Fixed Joystick").GetComponent<Joystick>();
joyStcik = GameObject.Find("Fixed Joystick");
joy = GameObject.Find("Jump Joystick").GetComponent<JoyButton>();
int playersInList = NetWorkPlayer.net.AllPlayers.Count;
int currentPlayers = PhotonNetwork.CurrentRoom.PlayerCount;
if (!NetWorkPlayer.net.AllPlayers.Contains(gameObject) )
{
// Player Adder
if(currentPlayers > 0)
{
Debug.Log("CurrentPlayers " + currentPlayers);
if(currentPlayers == 1)
{
if (playersInList <= 0)
{
GameObject NetWorkPlayerr = GameObject.Find("Network");
NetWorkPlayerr.GetComponent<NetWorkPlayer>().AllPlayers.Add(gameObject);
}
} else if(currentPlayers == 2)
{
if(playersInList <= 1)
{
GameObject NetWorkPlayerr = GameObject.Find("Network");
NetWorkPlayerr.GetComponent<NetWorkPlayer>().AllPlayers.Add(gameObject);
}
}
// Player Adder
// Checker
if(currentPlayers == 2)
{
if(playersInList >= 2)
{
if(!NetWorkPlayer.net.AllPlayers.ToArray()[0])
{
NetWorkPlayer.net.AllPlayers.RemoveAt(0);
PlayerHealthMulti.playerHealthMulti.playerNumber = 1;
GameObject NetWorkPlayerr = GameObject.Find("Network");
NetWorkPlayerr.GetComponent<NetWorkPlayer>().AllPlayers.Add(gameObject);
PV.RPC("RPC_PlayerKilled", RpcTarget.AllBuffered);
}
else if (!NetWorkPlayer.net.AllPlayers.ToArray()[1])
{
NetWorkPlayer.net.AllPlayers.RemoveAt(1);
PlayerHealthMulti.playerHealthMulti.playerNumber = 2;
GameObject NetWorkPlayerr = GameObject.Find("Network");
NetWorkPlayerr.GetComponent<NetWorkPlayer>().AllPlayers.Add(gameObject);
PV.RPC("RPC_PlayerKilled", RpcTarget.AllBuffered);
} else
{
Debug.Log("Nothing is null");
}
}
}
// Checker
}
}
Debug.Log(startTimeBtwShots);
timeBtwShots = startTimeBtwShots;
if (PV.IsMine)
{
EquipItem(0);
}
else
{
// Destroy(Timer);
Destroy(rb);
Destroy(ui);
}
}
public void moveRight()
{
// Debug.Log("f");
// transform.Translate(Vector2.right * Time.deltaTime);
}
void Update()
{
if (!PV.IsMine) return;
if(arrowL == null)
{
arrowL = GameObject.Find("ArrowLeft");
}
if(arrowR == null)
{
arrowR = GameObject.Find("ArrowRight");
}
if(arrowL != null)
{
if (arrowL.activeSelf == true && arrowR == true)
{
dirX = CrossPlatformInputManager.GetAxis("Horizontal") * runSpeed;
rb.velocity = new Vector2(dirX, rb.velocity.y);
}
}
if (gun1.activeSelf == true || rifleHolder1.activeSelf == true || shotGun1.activeSelf == true || knifeHolder1.activeSelf == true)
{
a.SetActive(true);
h.SetActive(false);
}
else
{
h.SetActive(true);
a.SetActive(false);
}
onGround = Physics2D.OverlapCircle(groundCheck.position, 1f, whatIsGround); // Checks if Y has changed since last frame
if (joyStcik.activeSelf == true)
{
rb.velocity = new Vector2(joystick.Horizontal * runSpeed, rb.velocity.y);
}
if (!jump && joy.Pressed && onGround)
{
jump = true;
rb.velocity += Vector2.up * 6f;
}
if (jump && !joy.Pressed)
{
jump = false;
}
if(shootButton.Pressed && timeBtwShots <= 0)
{
timeBtwShots = startTimeBtwShots;
items[itemIndex].Use();
}
if (timeBtwShots >= 0)
{
timeBtwShots -= Time.deltaTime;
}
}
void EquipItem(int _index)
{
itemIndex = _index;
items[itemIndex].itemGameObject.SetActive(true);
if(previousItemIndex != -1)
{
items[previousItemIndex].itemGameObject.SetActive(false);
}
previousItemIndex = itemIndex;
if(PV.IsMine)
{
Hashtable hash = new Hashtable();
hash.Add("itemIndex", itemIndex);
PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
}
}
public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
{
if(!PV.IsMine && targetPlayer == PV.Owner)
{
EquipItem((int)changedProps["itemIndex"]);
}
}
}
My health script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
using Photon.Pun.UtilityScripts;
public class PlayerHealthMulti : MonoBehaviour, IDamagable
{
public static PlayerHealthMulti playerHealthMulti;
[Range(0, 100)]
const float maxHealth = 100;
public static int scoreIntPlayer1 = 0;
public int scoreRefrence;
public Text player1Text;
PhotonView PV;
[Range(0, 100)]
public float currentHealth = maxHealth;
public int playerNumber;
public GameObject joysticks, score;
public HealthBarPlayer healthBar;
PlayerManager playerManager;
private void Awake()
{
PV = GetComponent<PhotonView>();
playerHealthMulti = this;
//currentHealth = maxHealth;
healthBar = GameObject.FindWithTag("Healthbar").GetComponent<HealthBarPlayer>();
playerManager = PhotonView.Find((int)PV.InstantiationData[0]).GetComponent<PlayerManager>();
SetScore();
}
void SetScore()
{
player1Text.text = scoreIntPlayer1.ToString();
}
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealthh(maxHealth);
PhotonNetwork.LocalPlayer.SetScore(0);
Debug.Log(currentHealth);
}
// Update is called once per frame
void Update()
{
if (!PV.IsMine) return;
scoreRefrence = scoreIntPlayer1;
SetScore();
if (joysticks == null)
{
joysticks = GameObject.FindWithTag("joyCan");
}
if (score == null)
{
score = GameObject.FindWithTag("st");
}
if (healthBar == null)
{
}
if (currentHealth >= 100)
{
currentHealth = 100;
StopCoroutine(ReHealth());
}
if (healthBar != null)
{
}
}
IEnumerator ReHealth()
{
yield return new WaitForSeconds(5);
currentHealth += 7;
StopCoroutine(ReHealth());
}
public void TakeDamge(float damage)
{
PV.RPC("RPC_TakeDamge", RpcTarget.All, damage);
}
[PunRPC]
void RPC_TakeDamge(float damage)
{
if (!PV.IsMine)
{
return;
}
currentHealth -= damage;
healthBar.SetHealthh(currentHealth);
if (currentHealth <= 0)
{
Die();
}
}
[PunRPC]
void RPC_PlayerKilled()
{
//if(!PV.IsMine)
// {
//scoreIntPlayer1++;
//PhotonNetwork.LocalPlayer.AddScore(scoreIntPlayer1);
//PhotonNetwork.LocalPlayer.AddScore(scoreIntPlayer1);
//UpdateText();
//}
int currentPlayers = PhotonNetwork.CurrentRoom.PlayerCount;
if(currentPlayers == 2)
{
if(playerNumber == 1)
{
scoreIntPlayer1++;
PhotonNetwork.CurrentRoom.GetPlayer(2).AddScore(scoreIntPlayer1) ;
Debug.Log(PhotonNetwork.CurrentRoom.GetPlayer(2).NickName + PhotonNetwork.CurrentRoom.GetPlayer(2).GetScore());
UpdateText();
} else if (playerNumber == 2)
{
scoreIntPlayer1++;
PhotonNetwork.CurrentRoom.GetPlayer(1).AddScore(scoreIntPlayer1);
Debug.Log(PhotonNetwork.CurrentRoom.GetPlayer(1).NickName + PhotonNetwork.CurrentRoom.GetPlayer(1).GetScore());
UpdateText();
}
}
}
public void UpdateText()
{
player1Text.text = PhotonNetwork.LocalPlayer.GetScore().ToString();
}
void Die()
{
playerManager.Die();
}
}
my score Ui
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using Photon.Pun.UtilityScripts;
public class ScoreUi : MonoBehaviourPunCallbacks
{
public RowUi rowUi;
public ScoreManager scoreManager;
PhotonView PV;
int scorePlayer;
private void Awake()
{
PV = GetComponent<PhotonView>();
}
void Start()
{
int currentPlayers = PhotonNetwork.CurrentRoom.PlayerCount;
Debug.Log(currentPlayers);
if(currentPlayers == 1)
{
float p1 = int.Parse(NetWorkPlayer.net.AllPlayers.ToArray()[0].GetComponent<PlayerHealthMulti>().player1Text.text);
scoreManager.AddScore(new Score(PhotonNetwork.CurrentRoom.GetPlayer(1).NickName, PhotonNetwork.CurrentRoom.GetPlayer(1).GetScore()));
}
if (currentPlayers == 2)
{
float p1 = int.Parse(NetWorkPlayer.net.AllPlayers.ToArray()[0].GetComponent<PlayerHealthMulti>().player1Text.text);
float p2 = int.Parse(NetWorkPlayer.net.AllPlayers.ToArray()[1].GetComponent<PlayerHealthMulti>().player1Text.text);
scoreManager.AddScore(new Score(PhotonNetwork.CurrentRoom.GetPlayer(1).NickName, PhotonNetwork.CurrentRoom.GetPlayer(1).GetScore()));
scoreManager.AddScore(new Score(PhotonNetwork.CurrentRoom.GetPlayer(2).NickName, PhotonNetwork.CurrentRoom.GetPlayer(1).GetScore()));
}
if (currentPlayers == 3)
{
scoreManager.AddScore(new Score(PhotonNetwork.CurrentRoom.GetPlayer(1).NickName, PlayerHealthMulti.scoreIntPlayer1));
scoreManager.AddScore(new Score(PhotonNetwork.CurrentRoom.GetPlayer(2).NickName, PlayerHealthMulti.scoreIntPlayer1));
scoreManager.AddScore(new Score(PhotonNetwork.CurrentRoom.GetPlayer(3).NickName, PlayerHealthMulti.scoreIntPlayer1));
}
var scores = scoreManager.GetHighScores().ToArray();
for (int i = 0; i < scores.Length; i++)
{
var row = Instantiate(rowUi, transform).GetComponent<RowUi>();
row.rank.text = (i + 1).ToString();
row.name.text = scores[i].name;
row.score.text = scores[i].score.ToString();
}
}
}
Timer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
using Photon.Realtime;
public class Timer : MonoBehaviourPunCallbacks
{
bool startTimer = false;
double timerIncrementValue;
double startTime;
public static double timer = 20;
public GameObject scoreBoard, scoreManager;
public TMP_Text timerUI;
ExitGames.Client.Photon.Hashtable CustomeValue;
void Start()
{
if (PhotonNetwork.LocalPlayer.IsMasterClient)
{
CustomeValue = new ExitGames.Client.Photon.Hashtable();
startTime = PhotonNetwork.Time;
startTimer = true;
CustomeValue.Add("StartTime", startTime);
PhotonNetwork.CurrentRoom.SetCustomProperties(CustomeValue);
}
else
{
startTime = double.Parse(PhotonNetwork.CurrentRoom.CustomProperties["StartTime"].ToString());
startTimer = true;
}
}
void Update()
{
if (!startTimer) return;
timerIncrementValue = PhotonNetwork.Time - startTime;
timerUI.text = timerIncrementValue + "";
if (timerIncrementValue >= timer)
{
timerIncrementValue = timer;
EndGame();
}
}
public void EndGame()
{
PlayerMulti.playerMulti.Allcanvas.SetActive(false);
scoreBoard.SetActive(true);
scoreManager.SetActive(true);
}
}
Comment