PhotonPlayer to gameObject?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

PhotonPlayer to gameObject?

Tomza
2018-06-01 13:29:11

How to get a gameObject of all players in the room? To get for example a component? It's seems simple but I cannot find the way to do that. A PhotonNetwork.playerList provides us with PhotonPlayers and now how to get their gameObjects?

Comments

Akrosh
2018-06-04 09:34:18

Hi @Tomza

Whenever a player connect you get response in void OnPhotonPlayerConnected(PhotonPlayer newPlayer) you have to instantiate a object and assign player details what ever you want to that object

Tomza
2018-06-04 09:48:51

Hi Akrosh, thank you for your answer.
For example, I have two players spawned in the game and I need to get a component, scripts, to call a function on them. How to do that?

lennart862
2018-06-04 23:43:48

Hi @Tomza,
that's easy to do:

Create a new gameobject in your scene. Name it "Network". Don't add a photon view to it.
Add a script to that game object. The script contains something like that:
public List AllPlayers = new List();

In the script which is attached to your player gameobject write:
GameObject Network = GameObject.Find("Network");
Network.GetComponent().AllPlayers.Add(gameObject);

If you want to get the player gameobject from another player do for example:

int HPOfPlayer2 = Network.AllPlayers.ToArray()[1].GetComponent().HP;

Tomza
2018-06-05 06:37:28

foreach(PhotonPlayer player in PhotonNetwork.playerList){  
MK_UIController mk_UIController = player.[SOMETHING].GetComponent<MK_UIController>();  
mk_UIController.AddMessage("Player " + other.NickName + " Left Game.");  
}

How to iterate through the list of players to get their components so that I can call a function on them? What should be instead of the SOMETHING placeholder so that the code works? Hope it is clear what I meant.

RP1_Nate
2019-04-29 01:19:01

@Tomza wrote:

foreach(PhotonPlayer player in PhotonNetwork.playerList){
MK_UIController mk_UIController = player.[SOMETHING].GetComponent<MK_UIController>();
mk_UIController.AddMessage("Player " + other.NickName + " Left Game.");
}

How to iterate through the list of players to get their components so that I can call a function on them? What should be instead of the SOMETHING placeholder so that the code works? Hope it is clear what I meant.

Did you ever figure this out? This is the EXACT issue I'm dealing with

Back to top