CommandAttribute doesn't work properly
Good day, guys. I'm working with UNET system, and cannot figure out why that piece of code doesn't work correctly:
using UnityEngine;
using UnityEngine.Networking;
using CnControls;
public class PlayerControl : NetworkBehaviour
{
public GameObject bulletPrefab;
public GameObject wallPrefab;
[Command]
public void CmdShoot()
{
Debug.log("Shooting");
}
[Command]
public void CmdWall()
{
Debug.log("Wall");
}
private void Update()
{
if (!base.isLocalPlayer) {
return;
}
if (Input.GetKeyDown("W")){
CmdWall ();
}
if (Input.GetKeyDown("S")){
CmdShoot ();
}
}
}
So the main problem is that CmdShoot and CmdWall are both being triggered serverside while CmdWall isn't triggering clientside. Any help is highly appreciated.
Answer by Salmjak · Feb 25, 2016 at 08:58 PM
[Command] runs functions on the server using variables from the client (as I've experienced it). [ClientRpc] run functions on the clients using variables from the server (as I've experienced it).
[Command] will only run on the server, not both server and client. To make it run on both server and client you can either:
1) Have a regular function that is also called when the [Command] is called (note that this will then only be run at the specific client in question and not on the other clients).
2) Call a [ClientRpc] within your [Command] to make the server send out the new state of the game to all clients.
Ok, I got it, but how would you explain that shooting command works on both client and server? I just don't really get it, is there are limits for commands?
I only want the server to instantiate the objects. Bullets are being instantiated correctly, however the walls arent instantiated on the client when I'm trying to.
Well, in your above code you only have Debug.Log() :P So as they are in your above example none of the logs should be shown client-side.
Have you attatched a networkidentity and added the prefab to "registered spawnable objects" for both of them? Have you tried to run the client in editor to see if the objects actually do/don't spawn? NetworkServer.Spawn() is not very good at some things (ex. it can't set a parent).
Sorry for that confusing moment :p
Yeah, I finally figured out what's been going wrong, the command itself was working just fine, I were taking raycast's position from the server camera. :S
Your answer
Follow this Question
Related Questions
Instantiating over the Network, good way? 1 Answer
Command function called on server 0 Answers
Player Object does not have authority? 1 Answer