I am trying to create a 2D top down multiplayer game where the players move and pickup objects.
Below is the spawning of 6 pick-up objects on a random 2D position. This script is attached to a game object in the scene called PickupSpawner.
public class PickupGen : NetworkBehaviour {
public GameObject pickup;
// Use this for initialization
public override void OnStartServer () {
print ("In OnStartServer");
float x, y;
for (int i = 0; i < 6; i++) {
x = Random.Range (-4.5f, 4.5f);
y = Random.Range (-4.5f, 4.5f);
Vector3 pos = new Vector3 (x, y,0);
GameObject go = (GameObject) Instantiate (pickup, pos,Quaternion.identity);
NetworkServer.Spawn (go);
}
}
}
The problem occurs when I try to destroy the objects after they are picked up. I am using the OnTriggerEnter2D with colliders as shown below. Note: All of this worked without network/multiplayer.
I have attempted to do this in many different ways. Below I am trying to use the [Command] attribute to tell the server to destroy the pickup object. I don't think I fully understand whats going on as I get this error after running and trying to pickup(destroy) a pickup object as the client(Note: It works fine as server(host)):
Found no behaviour for incoming [Command:InvokeCmdCmdDestroyPickup] on Player(Clone) (UnityEngine.GameObject), the server and client should have the same NetworkBehaviour instances [netId=9]. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
I have no idea what this means and I didn't get much insight from searching google for it. Any explanation would be appreciated. Thanks.
void OnTriggerEnter2D(Collider2D other) {
print ("In OnTrigger");
GameObject otherGO = other.gameObject;
if (otherGO.CompareTag ("Pickup")) {
// otherGO.SetActive (false);
// Destroy (otherGO);
CmdDestroyPickup (otherGO);
}
}
[Command]
public void CmdDestroyPickup(GameObject go){
NetworkServer.Destroy (go);
}