PhotonPlayer to gameObject?
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?
0
Best Answer
-
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;6
Answers
-
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?0 -
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;6 -
foreach(PhotonPlayer player in PhotonNetwork.playerList){
MK_UIController mk_UIController = player.[SOMETHING].GetComponent();
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.0 -
Did you ever figure this out? This is the EXACT issue I'm dealing withTomza said:foreach(PhotonPlayer player in PhotonNetwork.playerList){
MK_UIController mk_UIController = player.[SOMETHING].GetComponent();
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.0