[UNET] Client cannot call [Command]s, only the host can
I've searched all over the place and have tried numerous things to make this work. Here's the situation. I have the player press a button and it calls a Cmd function that instantiates a prefab and then sends the server the new gameobject using NetworkServer.Spawn(obj). This works great as the host; I can spawn the object and the other client can see it synced as well. However, when I do the same on the client, the Cmd function does not even run. I inserted print messages specifically into the command function to see if it was running, and it wasn't. All the code before and after the function call ran perfectly. It's as if the game decided to skip over the Cmd function because the client can't run those for some reason. Am I missing something here that I should know about client->server messages?
[command] functions only run on servers and not on clients.share your code so we can help you.
Thanks for the reply! I didn't know that [Command] functions could only be run on the server. How would a client send a message to the server then? I've looked over the API a bunch but haven't seen anything.
Here's the code that I'm trying to use. Basically, I want the client to send the server a gameobject (instantiated or prefab, doesn't matter) and then for the server to spawn that object so that it will appear for all clients.
[Command] public void CmdSpawn() { NetworkServer.Spawn(spawnedInstance);
}
And right before that gets called I have this:
spawnedInstance= Instantiate(spawnPrefab,
transform.position + new Vector3(0,4,0),
Quaternion.identity) as GameObject;
So what I'm looking for is a way for a client to tell the server to spawn an object and set a few settings on it as well.
Edit: Okay so I messed up the formatting but you should be able to get what I mean.
I have the same problem and I've been stuck for about a week now and I've seen tutorials where command runs on the client (for example spanning bullets). If you found a solution can you please share .
@Sruthin55 This is what my code looks like after I solved it. I forget how I did it (I think it was something with how I set the framework up).
//within FixedUpdate on a keypress
CmdSpawnPokemon(gameObject);
[Command]
void CmdSpawnPokemon(GameObject master) //Tells the server to spawn our pokemon, then sends it back to the player
{
GameObject go = Instantiate(pokemonPrefab, //Instantiate using prefabs
master.transform.position + new Vector3(0, 4, 0),
Quaternion.identity) as GameObject;
go.GetComponent<PokemonInfo>().owner = master; //Set the owner of the pokemon to the parameter sent by the client
go.GetComponent<Pokemon$$anonymous$$oveInput$$anonymous$$anager>().info.owner = master; //Do it again for fun
NetworkServer.SpawnWithClientAuthority(go, connectionToClient); //Spawn the gameobject on the server
RpcReceiveActivePokemon(go); //Send the gameobject back to the client so they can do stuff with it
}
Try something like that. Sorry that I'm not able to help more.
Oh I remember a bit more now. When I was debugging in the original post and said that the function didn't even run on the client, that was true. It was running on the server and I just wasn't spawning it correctly.
Your answer
Follow this Question
Related Questions
Simple NetworkView Question. 1 Answer
How to get OnTriggerEnter working over a network 2 Answers
Network Soccer Ball Position jitter, sending transform position late? 0 Answers
UNET AssetID 0000 zero 0 Answers
PLS recommend plugins for networking/sever which ONLY work for authentication + storing player info. 0 Answers