- Home /
Instantiate object from client doesn't work on server.
I want to be able to instantiate an object from remote and local clients. Tried for a while now, here is what I have instantiating it Transform InvTrans = m_Inventory.transform; Transform SlotTransform = Instantiate(inv.SlotPrefab, InvTrans.position, InvTrans.rotation, InvTrans.transform).transform; GameObject ObjGO = Instantiate(_object, SlotTransform.position, SlotTransform.rotation, SlotTransform); NetworkServer.Spawn(SlotTransform.gameObject); NetworkServer.Spawn(ObjGO);
It is being called in a command, on the client the guns spawn, but they are not parented under the slots, they are instantiated in world space. It works fine on the host. I get a warning on the client saying that I can't call the command, but I'm not sure how to do it any way else. I tried using a ClientRpc, it works better (code above but without the NetworkServer.Spawn()
's). Everything spawns for the host, but on the client, only his stuff spawns, the host's stuff doesn't. No errors or warnings are returning for the ClientRpc.
Answer by ray2yar · Dec 27, 2018 at 03:31 AM
I would use an array to hold the possible objects to spawn. Then I'd call a Command to spawn the object. Something like this:
public GameObject[] items;
void OnEquipItem()
{
//logic for which item to spawn
//send spawn info to the server
CmdEquip(item, parent, position);
}
[Command]
void CmdEquip(int i, Transform par, Vector3 pos)
{
GameObject anItem = Instantiate(items[i], pos, Quaternion.identity, par);
NetworkServer.Spawn(anItem);
}