NetworkServer.Spawn() not spawning object at correct position on Client-side with Mirror and Unity

294 views Asked by At

I'm making a card game using the mirror in unity and I came across a problem with 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:

public override void OnStartServer()
    {
        base.OnStartServer();
        cards = GenerateDeckInitial();
    }

    public override void OnStartClient()
    {
 
       Debug.Log("Cards CLIENT: " + cards.Count());
    }

    public SyncList<GameObject> GenerateDeckInitial() {
        SyncList<GameObject> cardsList = new SyncList<GameObject>();
        GameObject parentPosition = GameObject.Find("parentActions");
        foreach (var prefab in PrefabsActionCards)
        {
            Type ScriptCard = Type.GetType(prefab.name + ",Assembly-CSharp");
            var script = prefab.GetComponent(ScriptCard);
            var quantidade = (int)script.GetType().GetField("Quantidade").GetValue(script);
            for (int j = 0; j < quantidade; j++)
            {
                GameObject card = Instantiate(prefab, gameObject.transform.position ,gameObject.transform.rotation, gameObject.transform);          
                NetworkServer.Spawn(card);
                card.name = prefab.name.Replace("(Clone)", "");
                cardsList.Add(card);
            }
        }
        //ShuffleList(cardsList);
        //OrganizeCardHierarchy(cardsList, parentPosition, gameObject);
        return cardsList;
    }

This problem is only happening on the client side, with the host the objects spawn works fine. If anyone can help me, thank you!

1

There are 1 answers

0
iCannotBeYour King On

Same issue here but solved. In my case, the reason for the issue is that I set a parent object after spawning this object, and the parent object is a non-network object on the client side. The solution is to set this parent object to a network object and synchronize its position with the network transform, or reset its local position to zero.