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

Need some help figuring out networking and prefabs spawning children.

Discussion in 'Multiplayer' started by DarkOmeaga, Dec 4, 2016.

  1. DarkOmeaga

    DarkOmeaga

    Joined:
    Apr 12, 2016
    Posts:
    1
    Hey all, I've been learning how to work with the networking code of unity, but I'm having some problems.

    I'm building the player from multiple prefabs, it all starts with a single 'hardpoint' which can slot a single part of a space ship. parts have one or more hardpoints, which in turn can slot more parts, so i created a beautiful piece of code first converting json to the core information of the parts to an object, which in turn gets turned into a space ship. in single player it all works beautifully, but in multiplayer it seems that the spawned children don't have client authority, and thus, they can't spawn other objects

    Spawn code:
    Code (CSharp):
    1.     [Command]
    2.     void CmdSpawn()
    3.     {
    4.         heldInstance = Instantiate(holds, transform.position, transform.rotation) as GameObject;
    5.         heldInstance.transform.SetParent(transform);
    6.         NetworkServer.SpawnWithClientAuthority(heldInstance, transform.root.gameObject);
    7.     }
    Pretty straight forward, this code gets called from the hardpoint object, which, as i've said, is a child of the prefab spawned (in this case the variable 'holds', holds the prefab the hardpoint should spawn. Can anyone shed some light on what's happening here? Because im pretty lost at this point.
     
  2. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    Cmd and Rpc can only be called on components which share a GameObject with a NetworkIdentity component. And you can only have 1 per hierarchy (sort of). We got around this by having all our sub-items instantiate at the root of the hierarchy, so they can each be authoritative, but then parent them once they are fully spawned.