Reputation: 69
I'm trying to spawn a bullet when player hit screen. When I make server in matchmaking I can spawn bullets and I can see them on client side too. But when I connect like client to server on matchmaking I can't spawn bullets. I got this error on server side:
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()
My code:
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
}
I have this script on my player prefab. I have add on player and on bullet prefab Network Identity and Network Transform. On player I checked Local Player Authority.
I also tried to make a new project where I put everything from the official Unity Multiplayer Networking tutorial but it didn't work either.
Thanks for your help.
Upvotes: 3
Views: 1261
Reputation: 69
I figured it out. I was destroying this script if it wasn't attached to local player. I should only do-
if(!isLocalPlayer)
return;`
not
if(!isLocalPlayer)
{
Destroy(this);
return;
}
Upvotes: 0