Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party NetworkServer.Spawn() not spawning object at correct position Client-side using Mirror

Discussion in 'Multiplayer' started by Leon2306, Sep 29, 2023.

  1. Leon2306

    Leon2306

    Joined:
    Mar 9, 2020
    Posts:
    3
    So from the manual I was under the impression that NetworkServer.Spawn() would spawn the GameObject at the same position and rotation that I instantiated it at. However what I am seeing is that NetworkServer.Spawn() is sending all the objects to 0,0,0. Despite the fact that I am instantiating the objects at custom positions, shown in this code below:

    Code (CSharp):
    1. public override void OnStartServer()
    2.     {
    3.         base.OnStartServer();
    4.         cards = GenerateDeckInitial();
    5.     }
    6.     public override void OnStartClient()
    7.     {
    8.        Debug.Log("Cards CLIENT: " + cards.Count());
    9.     }
    10.     public SyncList<GameObject> GenerateDeckInitial() {
    11.         SyncList<GameObject> cardsList = new SyncList<GameObject>();
    12.         GameObject parentPosition = GameObject.Find("parentActions");
    13.         foreach (var prefab in PrefabsActionCards)
    14.         {
    15.             Type ScriptCard = Type.GetType(prefab.name + ",Assembly-CSharp");
    16.             var script = prefab.GetComponent(ScriptCard);
    17.             var quantidade = (int)script.GetType().GetField("Quantidade").GetValue(script);
    18.             for (int j = 0; j < quantidade; j++)
    19.             {
    20.                 GameObject card = Instantiate(prefab, gameObject.transform.position ,gameObject.transform.rotation, gameObject.transform);        
    21.                 NetworkServer.Spawn(card);
    22.                 card.name = prefab.name.Replace("(Clone)", "");
    23.                 cardsList.Add(card);
    24.             }
    25.         }
    26.         //ShuffleList(cardsList);
    27.         //OrganizeCardHierarchy(cardsList, parentPosition, gameObject);
    28.         return cardsList;
    29.     }
    This happening only client-side, in host player it's correct!!

    What is wrong?
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,339
    Yes, it's supposed to spawn at position + rotation.
    Please compare with our Tanks example and see how the projectile is spawned!