Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question (Mirror Networking) - How to parent a Spawned object?

Discussion in 'Multiplayer' started by cmurph255, Mar 1, 2024.

  1. cmurph255

    cmurph255

    Joined:
    Jan 23, 2024
    Posts:
    1
    I have a static gameobject in my scene used purely for organizational purposes (a transform to align objects for an AR application). All objects spawned by clients should be nested within the ImageTarget >> FineTuning >> MainBoard object in the scene. This parenting works fine when placing these objects as the host, but when done as another connected client it fails when performing the SetParent step:

    Code (CSharp):
    1. [Command]
    2. void CmdSpawnCirclePrefab(Vector3 spawnPosition)
    3.  {
    4.     currentCircle = Instantiate(circlePrefab, spawnPosition, Quaternion.identity);
    5.     NetworkServer.Spawn(currentCircle);
    6.     currentCircle.transform.SetParent(mainBoard.transform, worldPositionStays: true); //this line fails
    7. }
    On the connected client's side, the object is added directly to the scene, and an error is immediately thrown and the client is disconnected. On the host side I get the following error:

    Disconnecting connection: connection(1) because handling a message of type Mirror.CommandMessage caused an Exception. This can happen if the other side accidentally (or an attacker intentionally) sent invalid data. Reason: System.NullReferenceException: Object reference not set to an instance of an object
    Can anyone explain how to accomplish this?
     
  2. alvinoforiadjei

    alvinoforiadjei

    Joined:
    Feb 28, 2023
    Posts:
    2
    I am also interested in this.
     
  3. alvinoforiadjei

    alvinoforiadjei

    Joined:
    Feb 28, 2023
    Posts:
    2
    @cmurph255 I found an asnwer. All you need to do is to call a ClientRpc function and set the parent of your prefab there, not on the host itself. Because the host is a server and a client at the same time, it will also trigger it over there. So all your clients together with the host will get this prefab inside the parent like it should be. And also change the [Command] to [Server]

    So it will be like this:


    Code (CSharp):
    1. [Server]
    2. void CmdSpawnCirclePrefab(Vector3 spawnPosition) {
    3.     currentCircle = Instantiate(circlePrefab, spawnPosition, Quaternion.identity);
    4.     NetworkServer.Spawn(currentCircle);
    5.     RpcSpawnCirclePrefab(currentCircle);
    6. }
    7.  
    8.  
    9. [ClientRpc]
    10. void RpcSpawnCirclePrefab(GameObject currentCircle) {
    11.     currentCircle.transform.SetParent(mainBoard.transform);
    12. }
     
    nanderson940 likes this.
  4. SynoJ

    SynoJ

    Joined:
    May 14, 2019
    Posts:
    1
    Would this work even though new clients connect after the objects have been spawned?