Search code examples
unity-game-enginenetwork-programmingunity3d-unet

Command not trigger from client


This code, located on the player, reacts to a button press in a menu and the idea is when pressing the button "btn_MenuKill" the first time it become orange and the next time it change the color back to original yellow and do "print ("FIRE THE PROCESS")", in a SyncVar Hook "void ProcessKillObject(bool _MP_Orange) {".

Here is the situation/problem:

Start host in Editor

1) Click button on host-client (editor) = [Command] works

2) Click button on remote-client = Does not work, do not fire off the [Command]

Start host in generated "remote" client

1) Click button on host-client = [Command] works

2) Click button on remote-client = [Command] works

I do not understand why the [Command] does not work when the host is on the editor and i use the button on the remote.

Here is the code:

[SyncVar(hook = "ProcessKillObject")] public bool MP_KillOrange = false;
[SyncVar(hook = "MoveMainMenu")] public bool MP_MainMenu;
private NetworkIdentity objNetId;
private Color32 yellowButtonColor = new Color32 (200, 200, 2, 255);
private Color32 orangeButtonColor = new Color32 (255, 96, 0, 255);

public void Btn_Kill() {

    if (!isLocalPlayer)
        return;

    foreach (string objectTag in MP_Singleton.Instance.master_Object_List) {
        GameObject dummy = GameObject.Find (objectTag);
        Cmd_LocalAuthority (true, dummy);
    }

    Cmd_ProcessKill ();
}

// SyncVar Hook
void ProcessKillObject(bool _MP_Orange) {

    if (_MP_Orange) {
        GameObject.Find ("btn_MenuKill").GetComponent<Button> ().image.color = orangeButtonColor;
    } else if (!_MP_Orange) {
        GameObject.Find ("btn_MenuKill").GetComponent<Button> ().image.color = yellowButtonColor;

        print ("FIRE THE PROCESS");
    }
}

[Command] //>>>>>> THIS IS NOT TRIGGERED
void Cmd_ProcessKill() {

    // >>>>>>>>>>>>>>>>>>>>>>
    GameObject.Find ("MyText").GetComponent<Text> ().text = "HIT"; // TO SEE THAT THE COMMAD TRIGGER
    //>>>>>>>>>>>>>>>>>>>>>>>
    if (MP_KillOrange) 
        MP_MainMenu = !MP_MainMenu;

    MP_KillOrange = !MP_KillOrange;

}


[Command]
void Cmd_LocalAuthority(bool getAuthority, GameObject obj) {

    objNetId = obj.GetComponent<NetworkIdentity> ();        // get the object's network ID

    if (getAuthority) {
        objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player
    } else {
        objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player
    }
}

Solution

  • I added RPC and then it works.

    [ClientRpc]
    void Rpc_Position(GameObject myGO, float ranX, float ranY, int zDepth, float twist) {
    
        myGO.transform.position = new Vector3 (ranX, ranY, zDepth);
        myGO.transform.localEulerAngles = new Vector3(0f, 0f, twist);
    
    }