Re-parent networked game object
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).
Re-parent networked game object
atraver
2014-12-23 20:23:20
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.
Comments
Navigatron
2014-12-27 00:27:48
Your original code is instantiating the player prefab across the network, but only setting the parent on your local client. I was originally going to suggest RPCs, but there is a way without any network calls. Replace your original code with this, there is no need to save the gameobject, it parents itself. [code2=csharp]void OnJoinedRoom() { PhotonNetwork.Instantiate(playerPrefabName, Vector3.zero, Quaternion.identity, 0); }[/code2]
And on your playerPrefab, so that it will parent itself on every client as soon as it can:
[code2=csharp]public Transform prefabParent; void Awake(){ transform.parent = prefabParent.transform; }[/code2]
Thanks for the response, Navigatron. It seems as though that'll do the trick.
NomadicWarrior
2016-11-20 18:46:02
@Navigatron wrote:
Your original code is instantiating the player prefab across the network, but only setting the parent on your local client. I was originally going to suggest RPCs, but there is a way without any network calls. Replace your original code with this, there is no need to save the gameobject, it parents itself.
[code2=csharp]void OnJoinedRoom()
{
PhotonNetwork.Instantiate(playerPrefabName, Vector3.zero, Quaternion.identity, 0);
}[/code2]
And on your playerPrefab, so that it will parent itself on every client as soon as it can:
[code2=csharp]public Transform prefabParent;
void Awake(){
transform.parent = prefabParent.transform;
}[/code2]
Wait! I want to instantiate only once. First player who created a room he must instantiate object and set parent. I don't want other player instantiate object. So other player must see the object. They can't see because instantiated object does not parented on others(
Back to top