GameObject from PhotonPlayer

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.

GameObject from PhotonPlayer

ALC
2016-06-19 08:56:40

I want to be able to make a generic List of all of the players in the room. But using

foreach (PlayerUser up in PhotonNetwork.getPlayerList) {
//Mind you I'm on my phone writing this, you guys have the just of the code.
// How do I convert "up" or simply return the GameObject that the PlayerUser belongs to?

}

Comments

ahoogol
2016-06-19 15:04:43

In one of components of the GameObject override OnPhotonInstantiate and set GameObject to player.TagObject to access it later

ALC
2016-06-19 16:38:49

I called OnPhotonInstantiate(PhotonMessageInfo info)
inside of my player prefab and the Debug.Log doesnt appear in console. Any ideas?

ahoogol
2016-06-19 19:34:14

You dont need to direct call OnPhotonInstantiate, but you call PhotonNetwork.Instantiate and photon will run the callback defined in your prefab by OnPhotonInstantiate.
here it is an example:

  
public class PrefabAnyComponent : PunBehaviour  
{  
    public override void OnPhotonInstantiate(PhotonMessageInfo info)  
    {  
        //Assign this gameObject to player called instantiate the prefab  
         info.sender.TagObject = this.gameObject;  
    }  
}  

ALC
2016-06-19 20:59:44

I implemented it onto my Player, but when it instantiates, and I debug the gameObject's name, it returns the name of another of my gameobjects I have in the scene that implements all of my GUI and things for the login menu.

I think I could make a huge workaround, do you know of a way I could have a static List and store all of the players currently in the room? The biggest problem I've had with that, is the List isnt being synced to every player. Even if it's static. How do i keep 1 single list?

ahoogol
2016-06-19 21:20:29

Use PhotonNetwork.playerList to get the list of players in the current room.
To access all gameObjects instantiated in the scene:

  
var photonViews = UnityEngine.Object.FindObjectsOfType();  
foreach (var view in photonViews)  
{  
    var player = view.owner;  
    //Objects in the scene don't have an owner, its means view.owner will be null  
    if(player!=null){  
        var playerPrefabObject = view.gameObject;  
        //do works...  
   }  
}  

ALC
2016-06-20 04:49:02

That is genius! Thank you very much!

Back to top