- Home /
Unity [Command] function UNetWeaver error
I called a [Command] function but it is giving me this error : UNetWeaver error: Command function [GunController:CmdEuipGun] parameter [gunToEquip] is of the type [Gun] which is a Component. You cannot pass a Component to a remote call. Try passing data from within the component.
public class GunController : NetworkBehaviour {
public Transform gunHold;
Gun equippedGun;
public Gun startingGun;
public override void OnStartLocalPlayer() {
if (startingGun != null) {
CmdEuipGun(startingGun);
}
}
[Command]
public void CmdEuipGun(Gun gunToEquip) {
if(equippedGun != null) {
Destroy(equippedGun.gameObject);
}
equippedGun = Instantiate(gunToEquip, gunHold.position, gunHold.rotation) as Gun;
NetworkServer.Spawn(equippedGun.gameObject);
equippedGun.transform.parent = gunHold;
}
see at a bottom of this link.
you will find a arguments supported by the remote actions.
Answer by Bunny83 · Jan 17, 2019 at 03:05 AM
Just like storybelldev already mentioned in the comment above, the parameters of a command method need to be able to be serialized since it need to be send over the network. The documentation page hat a list of supported argument types at the very end. It also explicitly mentions that you can not use component types as argument:
Arguments to remote actions cannot be subcomponents of GameObjects, such as script instances or Transforms. They cannot be other types that cannot be serialized across the network.
Apart from that the error text couldn't be more clear. It clearly said what is wrong. If you want to pass a reference to an existing object, you have to use a GameObject reference. You can use GetComponent on the other side if you need to. Note that for instantiating prefabs it's usually easier to just pass an index into an array of prefabs which is available to both ends (server and client).