- Home /
[Command]-Function By Client is not called on Server
My PlayerObject has the following script:
[Command]
void CmdFire()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(
bulletPrefab, cameraRig.position, cameraRig.rotation);
// Spawn the bullet on the Clients
NetworkServer.Spawn(bullet);
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
The bullet prefab has a networkid, networktransform and its script is from networkingbehaviour. Prefab is registered in the NetworkManager. Everything works fine if the host-client runs the CmdFire() function. The bullet will spawn for all Clients.
But if I execute the CmdFire() function on a client, the code is never executed on the server, even though it should. No object is created on the server, not even for the host-client.
I am using Unity 2017.3.
Answer by anikint · Jul 19, 2018 at 11:34 PM
I think i found the error source:
cameraRig.position, cameraRig.rotation
as these are not SyncVars, Instantiate is not executed. Hence, Nullpointer exception occurs.
You should pass the required things as parameters to your command method. In your case you "may" pass the position and the rotation. Though it would be better to use some reference object on the server as spawn point. Otherwise clients could easily cheat by passing whatever they like to the method. We don't really know enough to answer your question . Networking can get quite complicated and highly depends on who has authority over which objects and what objects are involved.