Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

[Command] not working

Discussion in 'Connected Games' started by Mark-Sweeney, Aug 31, 2016.

  1. Mark-Sweeney

    Mark-Sweeney

    Joined:
    Feb 21, 2010
    Posts:
    169
    I can't get [Command] working for the life of me.

    Code (CSharp):
    1. public void PauseStartTime()
    2.     {
    3.         if(isServer) //we are the server
    4.         {
    5.             Debug.Log ("PauseStart() Fired");
    6.             timerIsOn = !timerIsOn;
    7.         }
    8.         else
    9.         {
    10.             Debug.Log ("Not the server - Call CMD");
    11.             CmdPauseStartTimer();
    12.         }
    13.     }
    14.  
    15.     [Command]
    16.     void CmdPauseStartTimer()
    17.     {
    18.         Debug.Log ("CMD called");
    19.         PauseStartTime();
    20.     }
    "Not the server - Call CMD" shows in the console, but void CmdPauseStartTimer() never fires as "CMD called" never shows up in the Console.

    I have network identity attached to the Player, Local Player Authority checked, but I can never get the client to fire a command to the server to toggle the timer on and off, only the host.
     
  2. isidro02139

    isidro02139

    Joined:
    Jul 31, 2012
    Posts:
    72
    Can you share the entire script? This is a NetworkBehaviour that is attached to the online Player prefab in the lobby manager's 'game player prefab' slot?

    Arun
     
  3. danrayson

    danrayson

    Joined:
    Mar 3, 2013
    Posts:
    10
    It's important to remember that [Command] can only be used in a script that's attached to a GameObject that has been passed into the...
    Code (CSharp):
    1. public static bool NetworkServer.AddPlayerForConnection(Networking.NetworkConnection conn, GameObject player, short playerControllerId);
    ...function.

    The NetworkManager can do this automagically for you if you tick the "Auto create player" option in the inspector.