无法从客户端执行服务器命令

3
我正在尝试在玩家击中屏幕时生成子弹。当我在匹配中创建服务器时,我可以生成子弹,并且客户端也可以看到它们。但是,当我像客户端一样连接到匹配服务器时,无法生成子弹。我在服务器端收到了以下错误信息:
“Found no behaviour for incoming [Command:InvokeCmdCmd_Fire] on Player(Clone) >(UnityEngine.GameObject), the server and client should have the same NetworkBehaviour instances [netId=2]. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()”
我的代码如下:
for (int x = 0; x < Input.touchCount; x++) 
        {
            touchpos = Camera.main.ScreenToWorldPoint(touch[x].position);
            if ((touchpos.x > -4.5f || touchpos.y > -1.2f))
            {
               pos = transform.position;

               if (magazine > 0)
                {
                    if (time > firetime && autoriffle)
                    {
                        Cmd_Fire();

                        time = 0;
                        magazine--;

                    }

        time += Time.deltaTime;

    }

    [Command]
    void Cmd_Fire()
    {      
        GameObject bullet = Instantiate(bulet, pos, Quaternion.FromToRotation(Vector3.up, new Vector3(touchpos.x, touchpos.y, transform.position.z) - transform.position)) as GameObject;

        bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.up * bulletspeed, ForceMode.Impulse);

        NetworkServer.Spawn(bullet);

        // I try NetworkServer.SpawnWithClientAuthority(bullet, connectionToClient); too but same reason
    }

我在我的玩家预制件上有这个脚本。我在玩家和子弹预制件上添加了网络标识和网络转换组件。在玩家上,我勾选了本地玩家权限。(Local Player Authority)
我也尝试过创建一个新项目,在其中将官方Unity多人游戏网络教程中的所有内容放置在一起,但仍然无法正常工作。
感谢您的帮助。
1个回答

0
我明白了。如果这个脚本没有连接到本地玩家,我会将其销毁。我应该只进行-
if(!isLocalPlayer)
   return;`

不是

if(!isLocalPlayer)
{
   Destroy(this);
   return;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接