Getting the game object and component in foreach? (multiplayer)

Hi,

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?

*The commented lines do not work.

Can anybody help me with this? All help is greatly appreciated

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();
1 Like

[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

Hey did you ever figure this one out? Trying to get components from the photon Player

The error tells you exactly what’s wrong. They’re trying to call GetComponentInParent(), which doesn’t exist in the PhotonPlayer class.

did you guys ever figured it out ? I am trying this and it doesn’t work :

playerConversant = GameObject.FindGameObjectWithTag(“Player”).GetComponent();

I am able to find the GameObejct with the tag but GetComponent doesn’t find the PlayerConversant script… any ideas??

That is a completely useless statement.

Also, please don’t necro-post to ancient threads unrelated to your problem.

Start your own thread and do it properly… it’s FREE!

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

Necroposting:
https://www.youtube.com/watch?v=1kUlD7OWpLM