I am tired as I am searching for the solution for hours
Now I have a list which contain the Amount of players in a scene (I am using Photon Pun 2). Now I made it everytime player join the scene is added automatically to the list. Now when I need to display the score of players it gives me this error (IndexOutOfRangeException: Index was outside the bounds of the array.)
My codes
in PlayerController
GameObject NetWorkPlayerr = GameObject.Find("Network");
NetWorkPlayerr.GetComponent<NetWorkPlayer>().AllPlayers.Add(gameObject);
In my list
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class NetWorkPlayer : MonoBehaviour {
public static NetWorkPlayer net;
private void Awake()
{
net = this;
}
public List<GameObject> AllPlayers = new List<GameObject>();
}
in my healthScript (Containing The Score)
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 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);
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)
{
PV.RPC("RPC_PlayerKilled", RpcTarget.All);
Die();
}
}
[PunRPC]
void RPC_PlayerKilled()
{
if(!PV.IsMine)
{
scoreIntPlayer1++;
Debug.Log("Got a score: " + scoreIntPlayer1 + "Text will be: " + player1Text.text);
} else
{
Debug.Log("Its me orginal");
}
}
void Die()
{
playerManager.Die();
}
}
In my scoreUi
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class ScoreUi : MonoBehaviour
{
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)
{
Debug.Log(int.Parse(NetWorkPlayer.net.AllPlayers.ToArray()[1].GetComponent<PlayerHealthMulti>().player1Text.text));
float p1 = int.Parse(NetWorkPlayer.net.AllPlayers.ToArray()[1].GetComponent<PlayerHealthMulti>().player1Text.text);
Debug.Log(p1);
scoreManager.AddScore(new Score(PhotonNetwork.CurrentRoom.GetPlayer(1).NickName, p1));
}
if (currentPlayers == 2)
{
PlayerHealthMulti p1H = NetWorkPlayer.net.AllPlayers.ToArray()[1].GetComponent<PlayerHealthMulti>();
PlayerHealthMulti p2H = NetWorkPlayer.net.AllPlayers.ToArray()[2].GetComponent<PlayerHealthMulti>();
int p1 = p1H.scoreRefrence;
int p2 = p2H.scoreRefrence;
scoreManager.AddScore(new Score(PhotonNetwork.CurrentRoom.GetPlayer(1).NickName, p1));
scoreManager.AddScore(new Score(PhotonNetwork.CurrentRoom.GetPlayer(2).NickName,p2));
}
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();
}
}
}
Now What should I do??