- Home /
Unet: Spawner spawns for every player one object instead of just one
Hey guys,
I'm currently facing a problem with my UNET implementation of Real Time Strategy game. My problem is the following:
I'm using a Spawner for every player (it should be a 1 on 1 game) who should manage spawning the GameObjects of the Player (Creatures). Every player owns some Creatures, they have LocalAuthority in my project. The problem I get is that if I connect my client to my Host and try to spawn some creatures (right now by pressing enter), the server spawns one object for itself and one from the client, this is one too much.
I tried a lot, but I didn't get it the way it should work, maybe I'm understanding something wrong, here, I'm not sure. Here are my code snippets which are called:
if (Input.GetKeyDown ("enter")) {
Creature c = new Creature();
c.setLifePoints(100);
c.setHunger(100);
c.setFatigue(100);
c.setAttackValue(100);
c.setDefensiveValue(50);
if(base.isServer){
CmdCreateCreature(CreatureConverter.creatureToStructConverter((Creature)c));}
}
[Command]
public void CmdCreateCreature(CreatureConverter.CreatureStruct cs) {
//Calling [ClientRpc] on the server.
Debug.Log ("Spawning");
RpcCreate(cs);
}
[ClientRpc]
public void RpcCreate(CreatureConverter.CreatureStruct c) {
//Debug.Log("RPC: This is " + (this.isServer ? " Server" : " Client"));
if (this.isServer) {
GameObject obj = MonoBehaviour.Instantiate(this.spawnPrefab) as GameObject;
// setting Values of the obj fitting to the creature here
NetworkServer.SpawnWithClientAuthority(obj, this.connectionToClient);
CmdAddToList(obj);
}
else {
//Debug.Log("Testing."); Doesnt happen..
}
}
Note: The Input to spawn a Creature is in the FixedUpdate method.
In my project I'm using a SpawnerPrefab with the Spawner Script (which you can read the important parts of above) and a Creature GameObject with NetworkIdentys etc, these things should (hopefully) don't be the problem. If you need further information, feel free to ask!!
Thanks for reading and thanks in advance for your answers!
TL;DR: My Host spawns GameObjects for every client connected and I have no clue why.