Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Parent-Child Problems in Network with RPC

Discussion in 'Multiplayer' started by canazn, Feb 21, 2014.

  1. canazn

    canazn

    Joined:
    Jan 5, 2014
    Posts:
    4
    Hey guys, I was wondering if you could help me. I spent several hours trying to get this to work and searching online but haven't came across an answer for this.

    I want to make a parent-child relationship between 2 gameObjects. And this is the code where I'm doing it from :

    void Start () {
    if(networkview.isMine) {
    newChild = Network.Instantiate(prefab, spawnLocation.position, spawnLocation.rotation, 0) as GameObject;
    addToParent(networkView.viewID);
    }
    }


    [RPC] void addToParent(NetworkViewID id) {

    newChild.transform.parent = NetworkView.Find(id).transform;

    if (networkView.isMine)
    networkView.RPC ("addToParent", RPCMode.OthersBuffered, id);
    }

    So when I run this on 2 instances, player1's computer with his controlled character will see in the "hierarchy" that his character will have a child object but will see that the other character (controlled by player2) will not. Meaning, both the supposed child and parent gameObjects of player2 will be in the "hierarchy" but not as a parent-child. This is the same on player2's computer where his corresponding character will have a parent-child gameObject and player1's computer will be incorrect.

    I hope you guys can help explain to me what I'm doing wrong as I really need to fix this. Thanks!
     
  2. emre2345DH

    emre2345DH

    Joined:
    Apr 17, 2013
    Posts:
    28
    Don' t you have to use networkview.RPC("addToParent",....) ?
     
  3. canazn

    canazn

    Joined:
    Jan 5, 2014
    Posts:
    4
    Thanks for replying. I do, I called it in my addToParent() function. I tried calling it in start() anyways cause what do I have to lose, but it still doesn't work.
     
  4. emre2345DH

    emre2345DH

    Joined:
    Apr 17, 2013
    Posts:
    28
    Ah sorry.

    You want to instantiate a gameobject over network and make child the created one of another.

    This should do the work:

    Don' t forget adding a networkView to the prefab you want to create.

    Code (csharp):
    1.  
    2.  
    3. void Start () {
    4. if(networkview.isMine)
    5. {
    6. newChild = Network.Instantiate(prefab, spawnLocation.position, spawnLocation.rotation, 0) as GameObject;
    7. newChild.transform.parent = transform;
    8. networkView.RPC("addToParent", RPCMode.OthersBuffered, newChild.networkView.viewId);
    9. }
    10. }
    11.  
    12.  
    13. [RPC] void addToParent(NetworkViewID id) {
    14.  
    15. Transform t = NetworkView.Find(id).transform;
    16. t.parent = transform;
    17. Destroy(t.networkView);
    18. }
    19.  
    20.  
     
  5. canazn

    canazn

    Joined:
    Jan 5, 2014
    Posts:
    4
    Thanks so much, It works now!

    I did it wrong that I was always passing the networkView.viewId of the parent object.. when I should have been passing the viewId of the child.

    Thanks so much again and I hope this thread will help out other new unity networkers too!
     
  6. Superfan99

    Superfan99

    Joined:
    Feb 14, 2013
    Posts:
    3
    Any chance someone could translate the above into js?