# Calling a function @ server from client ?

**URL:** https://discussions.unity.com/t/calling-a-function-server-from-client/599039
**Category:** Multiplayer & Networking
**Created:** [September 25, 2015, 4:32pm UTC](https://discussions.unity.com/t/calling-a-function-server-from-client/599039 "2015-09-25T16:32:58Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Muckel](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/muckel/32/523503_2.png) [@Muckel](https://discussions.unity.com/u/Muckel)
#### Post date: [September 25, 2015, 4:32pm UTC](https://discussions.unity.com/t/calling-a-function-server-from-client/599039/1 "2015-09-25T16:32:58Z")

</div>

Hello,  
i just try to figure out how to call a function from a client on the server?  
to call a function on a client is not a problem with [ClientRpc]…  
but how to do the opposite ?  
call a function on the server from client?  
maybe it’s simple but i can not find a answer on iNet…  
many thx  
M.

---

<div class="post-metadata">

### Author: ![RoystanHonks](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/roystanhonks/32/708245_2.png) [@RoystanHonks](https://discussions.unity.com/u/RoystanHonks)
#### Post date: [September 25, 2015, 5:05pm UTC](https://discussions.unity.com/t/calling-a-function-server-from-client/599039/2 "2015-09-25T17:05:52Z")

</div>

You use a [[Command]](http://docs.unity3d.com/Manual/UNetConcepts.html). Bear in mind only scripts with local player authority are allowed to call Commands.

---

<div class="post-metadata">

### Author: ![Muckel](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/muckel/32/523503_2.png) [@Muckel](https://discussions.unity.com/u/Muckel)
#### Post date: [September 25, 2015, 5:41pm UTC](https://discussions.unity.com/t/calling-a-function-server-from-client/599039/3 "2015-09-25T17:41:15Z")

</div>

Hi,  
thx for quick reply but that doesn’t work…  
that was what i tried the day…  
here is the function:

```csharp
//call this on the client
public void setBtn(){ 
    CmdCall();
    }
//nothing happen @ the server...
    [Command]
    private void CmdCall() {
    print("CmdCall !!!");
}

```

thx  
M.

---

<div class="post-metadata">

### Author: ![Huntibia](https://avatars.discourse-cdn.com/v4/letter/h/bcef8e/32.png) [@Huntibia](https://discussions.unity.com/u/Huntibia)
#### Post date: [September 25, 2015, 7:31pm UTC](https://discussions.unity.com/t/calling-a-function-server-from-client/599039/4 "2015-09-25T19:31:12Z")

</div>

Did you attach this script to a gameobject with client authority (usually the player prefab where you checked “Local Player Authority” in the NetworkIdentity)?

I wrote an example of spawning with client authority for another guy that might help you as well. See the attached zip.

[2312149–155797–TestSpawn.rar](https://discussions.unity.com/uploads/short-url/jE3gUImseY7WDBXHqis4vYi4DQn.rar) (41.6 KB)

---

<div class="post-metadata">

### Author: ![pakfront](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/pakfront/32/359658_2.png) [@pakfront](https://discussions.unity.com/u/pakfront)
#### Post date: [September 25, 2015, 11:33pm UTC](https://discussions.unity.com/t/calling-a-function-server-from-client/599039/5 "2015-09-25T23:33:27Z")

</div>

To keep things simple, if you can, have all the [Command] methods on a NetworkBehaviour attached to the same GameObject as the player. That way you have client authority by default for that NetworkBehaviour.

This is also recommend in the docs:  
Commands are sent from player objects on the client to player objects on the server. For security, Commands can only be sent from YOUR player object.  
[http://docs.unity3d.com/Manual/UNetActions.html](http://docs.unity3d.com/Manual/UNetActions.html)

---

<div class="post-metadata">

### Author: ![Muckel](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/muckel/32/523503_2.png) [@Muckel](https://discussions.unity.com/u/Muckel)
#### Post date: [September 26, 2015, 7:15am UTC](https://discussions.unity.com/t/calling-a-function-server-from-client/599039/6 "2015-09-26T07:15:24Z")

</div>

Hello @ all,  
many thx for the quick reply’s!  
Respect.  
Well maybe i was not clear enough…  
I have a Button on Server and Client… now if i press that btn on the Server it change the color to red also on client.  
So [ClientRpc] works!  
Now if i push that Button on the **Client** it should also change the state of the btn and color on the Server!  
That doesn’t work and i can’t get it to run…  
I gave the client isAuthority but nothing happened… The command doesn’t get executed on the Server…  
So basically if i push the btn on server it reacts also on client but if i push the btn on client with the [Command] nothing happen on Server… hmmm is this a bug?  
Like i can switch the light on from server to client but i can not switch the light from client to server…  
So if i’m a client i cannot switch out the light on the Server?  
many many thx  
M.

Hm maybe it is a issue still…

> **[Unity IssueTracker - ClientRPC isnt called for local client for networked...](https://issuetracker.unity3d.com/issues/clientrpc-isnt-called-for-local-client-for-networked-scene-objects)**
>
> ClientRPC doesnt work for network objects placed in the scene.

---

<div class="post-metadata">

### Author: ![pakfront](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/pakfront/32/359658_2.png) [@pakfront](https://discussions.unity.com/u/pakfront)
#### Post date: [September 28, 2015, 5:41pm UTC](https://discussions.unity.com/t/calling-a-function-server-from-client/599039/7 "2015-09-28T17:41:26Z")

</div>

I do have something similar working, at least in the case that the NetworkBehaviour is part of the “PlayerPrefab.”

---

<div class="post-metadata">

### Author: ![haouari](https://avatars.discourse-cdn.com/v4/letter/h/958977/32.png) [@haouari](https://discussions.unity.com/u/haouari)
#### Post date: [January 16, 2020, 11:46am UTC](https://discussions.unity.com/t/calling-a-function-server-from-client/599039/8 "2020-01-16T11:46:23Z")

</div>

i think i know how to fix it  
give that btn a script witch has [Varsync(hook=“RpcOnvaluechange”)] public bool isred or change it to something more open if you want . but you can’t sync gameobjects , anyways… make it monobehaviour and attach if statment in the update function that says if its true than change color . now… go to your player script and everytime you click on that btn tell it to give athority to the btn and change the color by command then take the athority away … after that  
“RPcOnvaluechange” function with [ClientRpc] before it … says that RpcOnvaluechange(newvalue){isred=newvalue;}  
this function gonna exucate on every client changing the value when it changes in the server

sorry for my bad writting skills … ik its a little bit late . but i hope it helps somone else
