GameObject from PhotonPlayer
The whole answer can be found below.
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).
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
In one of components of the GameObject override OnPhotonInstantiate and set GameObject to player.TagObject to access it later
I called OnPhotonInstantiate(PhotonMessageInfo info)
inside of my player prefab and the Debug.Log doesnt appear in console. Any ideas?
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;
}
}
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
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...
}
}
That is genius! Thank you very much!
Back to top