0

I need help running functions on all clients and the host.

I tried the below code but it only worked on the host not on clients.

In this code I have made a jump up skill. When players take it, they can use it by pressing R button. It works fine in host but on clients doesn't work.

[Command]
void Cmd_ProvideJumpToserver()
{
    Rpc_JumpUp();
}
[ClientRpc]
void Rpc_JumpUp()
{
    if (JumpUp == 1)
    {
        Debug.Log("Jump Up Used");
        gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * 15f, ForceMode2D.Impulse);
        JumpUp = 0;
    }
}
3
  • Well I guess something has to set JumpUp to one.
    – AgentFire
    Commented Aug 9, 2019 at 8:03
  • Yes, when they take the skill from map, JumpUp variable gets to one nad also i try it by taking from map and giving from inspector but it doesn't work. By the way i getting the Debug.Log("Jump Up Used"); message on console.
    – vNone
    Commented Aug 9, 2019 at 8:07
  • could you show us how you set the JumpUp to 1? also you get the Jump Up Used method on the client or only the host?
    – derHugo
    Commented Aug 9, 2019 at 10:56

1 Answer 1

0
public class Move : NetworkBehaviour
{
    private void Start()
    {
        if (isLocalPlayer) return;
        // Here we closed the script which is not ours
        GetComponent<Move>().enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            CmdComand();
        }
    }

    [Command]
    private void CmdComand()
    {
        // We give it to server
        RpcToClients();
    }

    [ClientRpc]
    private void RpcToClients()
    {
        // Here we do what we want to happen on all player
        GetComponent<Rigidbody2D>().AddForce(Vector2.up * 5,ForceMode2D.Impulse);
    }
}

Thanks guys who try to help me :))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.