I'm trying to re-parent a remotely created game object after it has been instantiated by Photon, and I'm looking for sort of a best practice. Locally, this is what I'm looking at:
[code2=csharp]void OnJoinedRoom()
{
GameObject playerGameObject = PhotonNetwork.Instantiate(playerPrefabName, Vector3.zero, Quaternion.identity, 0);
playerGameObject.transform.SetParent(prefabParent.transform, false); // prefabParent is an empty container game object serialized through the Unity Editor
}[/code2]
This re-parents my prefab underneath the players' container object (set to the prefabParent game object). However, when a second client joins my room, its game object is instantiated at the root of my object hierarchy. Seemingly, then, I'd need to add code that finds the networked player's game object based on a given PhotonPlayer object, then sets the parent accordingly. Something like this:
[code2=csharp]void OnPhotonPlayerConnected(PhotonPlayer photonPlayer)
{
GameObject playerGameObject = PhotonView.Find(photonPlayer.ID).gameObject;
playerGameObject.transform.SetParent(prefabParent.transform, false);
}[/code2]
That doesn't seem to be working, though, maybe because the PhotonPlayer's ID is not the same as the PhotonView's ID? I've looked at PhotonPlayer and PhotonNetwork but neither seem to have Find methods that will give me what I'm looking for.
I know I can find the object using Unity's GameObject.Find method, but I'd rather not go down that route because it assumes I know too much about the prefab at compile-time (i.e., it seems messy).
Any help would be appreciated.
[code2=csharp]void OnJoinedRoom()
{
GameObject playerGameObject = PhotonNetwork.Instantiate(playerPrefabName, Vector3.zero, Quaternion.identity, 0);
playerGameObject.transform.SetParent(prefabParent.transform, false); // prefabParent is an empty container game object serialized through the Unity Editor
}[/code2]
This re-parents my prefab underneath the players' container object (set to the prefabParent game object). However, when a second client joins my room, its game object is instantiated at the root of my object hierarchy. Seemingly, then, I'd need to add code that finds the networked player's game object based on a given PhotonPlayer object, then sets the parent accordingly. Something like this:
[code2=csharp]void OnPhotonPlayerConnected(PhotonPlayer photonPlayer)
{
GameObject playerGameObject = PhotonView.Find(photonPlayer.ID).gameObject;
playerGameObject.transform.SetParent(prefabParent.transform, false);
}[/code2]
That doesn't seem to be working, though, maybe because the PhotonPlayer's ID is not the same as the PhotonView's ID? I've looked at PhotonPlayer and PhotonNetwork but neither seem to have Find methods that will give me what I'm looking for.
I know I can find the object using Unity's GameObject.Find method, but I'd rather not go down that route because it assumes I know too much about the prefab at compile-time (i.e., it seems messy).
Any help would be appreciated.