How do you set the parent object of a PhotonNetwork.InstantiateSceneObject?
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).
How do you set the parent object of a PhotonNetwork.InstantiateSceneObject?
roskelld
2016-12-01 17:19:38
I've been populating my level with Instantiated scene objects, but no matter where I call the code from in the scene structure, the objects become children of the root.
How do I instantiate them as children of another scene object? I have data in the desired parent that I need them to inherit.
Comments
Hi,
you'll have to do that on each instance yes, parenting is something you have to do manually because we can't pass a gameobject reference directly over the network.
so when a instance is spawned, either it is responsible to find its parent and attached itself to it, or else a manager has to watch for this objects and attached them appropriately.
Bye,
Jean
The way I do it is I instantiate the object and then immediately call an RPC called doEnable();
Within doEnable(), it picks up parent object, syncs itself with the game, etc. For instance each player has a different color each time you spawn. The game manager has a list of colors and keeps track of which color is correct, and then I call doEnable(int) right after instantiating. THe RPC comes through on everyone's client, and the method looks something like:
[punRPC]
public void doEnable (int playerColor)
{
meshRenderer.material.color = gameManager.colors[playerColor];
}
You could cache references to all the objects that will serve as parents in the gameManager, and then parent them in an RPC like
[punRPC]
public void doEnable (int parent)
{
transform.SetParent(gameManager.parentObjects[parent]);
}
Thanks for the info steamcore.
Back to top