Setting a parent of the new object

Options
How to set a parent the object via RPC? I'm trying to create a system of building, and to not use a lot of viewID. I decided to make a synchronization system in a special way, but this requires move an object to a parent with a special script.

Comments

  • Xytabich
    Options
    Up.
    Does anyone know the answer?
  • Benn
    Options
    You may want to provide a little more information about your "special way" but the concept of reparenting an object via RPC is pretty straightforward.

    To begin, you need to have a handle for each object, both the parent and the child. Here's the way I would do it. In the child object with the photonView component I would establish photonView in Start() as
    photonView = GetComponent();
    You should also establish the parent somewhere. If there are multiple potential parent objects you need a way to find which parent to use. I use a naming convention where in the beginning of the script each potential parent object "checks-in" with a mastercontroller object that contains a Dictionary of pairs. Each parent generates a new unique name and the mastercontroller creates an entry in the Dictionary with the name as the string and the script that controls the object as the value. Since all parent objects in my scene can then be found by their name and these names are synced across the network when they are instantiated all I need to pass over the network is the command to reparent via RPC along with the string name of the object I want to parent to.

    Example:
    void SetParent(ControlScript parent) {
    string name = parent.objectname;
    photonView.RPC("NetworkSetParent", PhotonTargets.All, name);
    }

    [PunRPC]
    void NetworkSetParent (string name) {
    ControlScript script = MasterController.GetObjectFromName(name);
    transform.parent = script.transform;
    }
  • Xytabich
    Options
    Mostly clear, thanks.