I’m making a simple scoreboard for my multiplayer game that displays a fixed score based on when players die.
In each scene I’ve got a script that lists each instantiated player with their respective names. My script below is set to instantiate a prefab that contains the players name and score as a child object of a canvas in the scene that everyone can see:
foreach (PhotonPlayer p in PhotonNetwork.playerList)
{
string nickName = p.NickName;
//PhotonPlayer p = p.GameObject.GetComponent<Health>();
//p.GameObject.GetComponent<Health>();
GameObject go = PhotonNetwork.Instantiate(PSEntry.name, transform.position, Quaternion.identity, 0);
go.transform.SetParent(this.transform);
go.transform.Find("Username").GetComponent<Text>().text = p.NickName;
go.transform.Find("Points").GetComponent<Text>().text = "22222";
}
My problem is that I can’t acces each players Health script to check if they’re dead because PhotonPlayer is not the actual game object. How can I access the game object of each member in the player list?
I don’t know how photon works, but i think you ca do something like this.
float hp = 0;
var p = p.GameObject.GetComponent<Health>();
if (p != null)
{
hp = p.GetHP();//GetHP it's a method from your Health Script
}
or …
GameObject go = PhotonNetwork.Instantiate(PSEntry.name, transform.position, Quaternion.identity, 0);
hp = go.transform.gameObject.GetComponent<Health>().GetHP();
[quote=“adi7b9, post:3, topic: 778858, username:adi7b9”]
I don’t know how photon works, but i think you ca do something like this.
float hp = 0;
var p = p.GameObject.GetComponent<Health>();
if (p != null)
{
hp = p.GetHP();//GetHP it's a method from your Health Script
}
or …
GameObject go = PhotonNetwork.Instantiate(PSEntry.name, transform.position, Quaternion.identity, 0);
hp = go.transform.gameObject.GetComponent<Health>().GetHP();
[/quote] Thanks for trying to help. I need to acces the script Health on the already instantiated players though, it’s not accessible in the foreach I mentioned because PhotonPlayer is not a game object referance.
Okay so I’ve gotten a little further now. In an Awake function on each player with a photon view component on them, I’ve written this line of code:
photonView.owner.TagObject = gameObject;
This should supposedly make the Game Object of each player accessible in a PhotonNetwork.playerList.
So in another script on an empty gameopbject in my scene, I check whos in the scene, THEN I use TagObject ToString, but I still can’t access the game object :
foreach (PhotonPlayer p in PhotonNetwork.playerList)
{
string nickName = p.NickName;
p.TagObject.ToString();
if (p.GetComponentInParent<Health>().dead1True)
{
//Instantiate the PlayerScoreEntry prefab (PSEnty.name) if the player is dead. This is not the player, merely a high score table entry.
GameObject go = PhotonNetwork.Instantiate(PSEntry.name, transform.position, Quaternion.identity, 0);
go.transform.SetParent(this.transform);
go.transform.Find("Username").GetComponent<Text>().text = p.NickName;
go.transform.Find("Points").GetComponent<Text>().text = "22222";
}
}
The error persists in line “if (p.GetComponentInParent().dead1True);” It says -PhotonPlayer does not contain definition for GetComponentInParent…Are you missing an assembly reference…
What am I doing wrong here? I would really appreciate the hel if anyone knows how to make this work. Thank you