- 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 hexagonius · Dec 21, 2018 at 06:27 AM
this is the where UNET gets complicated.
1. the command you're talking about must be called either on the networkplayer or a go with NetworkIdentity and authority.
2. there are three ways to handle client side stuff with Network gameobjects. recall, all they got was the order to instantiate an object, the parenting only happened on the server and since these objects, as you noticed, are rather coming from nowhere on the client, those don't know about any other go just yet. what can you do. you can either let the gun add itself to a slot by searching the scene for it. if the gun has nothing to sync over network, you can instantiate or locally on each client and make the networkinstantiate a clientrpc telling the clients to do that. OR you can register the gun on the clients to receive a callback for them containing the spawned instance (like you would get from instantiate itself) like this: