- Home /
NetCodeForGameobjects Rpc for a specific client
I'm using netcode for gameobjects for my multiplayer game,I use this code to spawn an object in clients
[ClientRpc]
public void test1ClientRpc()
{
GameObject ob = Instantiate(Obj,new Vector3(0,0,0),Quaternion.Euler(0,0,0));
}
but the problem is that it sends this code to all clients, I want it to send it for a specific client.
now I'm using this code,
[ClientRpc]
public void test1ClientRpc()
{
if (NetId==2)
{
GameObject ob = Instantiate(Obj, new Vector3(0, 0, 0), Quaternion.Euler(0, 0, 0));
}
}
it works fine but it has a problem .server still sends the code to all clients,then they check the condition and .... , I want to send it only for that specific client :)
(My guess is that) RPC can send arguments over the network.
[ClientRpc]
public void test1ClientRpc ( int specificNetId )
{
if( this.NetId==specificNetId )
{
/* code */
}
}
yes but as I said in this case server has to send it to all clients.then they have to check the condition :) want to send it for a specific client not all clients :)
Answer by andrew-lukasik · Apr 20 at 01:19 PM
[ClientRpc]
public void MySpecificClientRpc ( ClientRpcParams clientRpcParams )
{
/* code */
}
private void DoSomethingServerSide(ulong clientId)
{
// If is not the Server/Host then we should early return here!
if (!IsServer) return;
// NOTE! In case you know a list of ClientId's ahead of time, that does not need change,
// Then please consider caching this (as a member variable), to avoid Allocating Memory
every time you run this function
ClientRpcParams clientRpcParams = new ClientRpcParams
{
Send = new ClientRpcSendParams
{
TargetClientIds = new ulong[] { clientId }
}
};
DoSomethingClientRPC(clientRpcParams);
}
[ClientRpc]
private void DoSomethingClientRPC(ClientRpcParams clientRpcParams = default)
{
if (IsOwner) return;
// Run your client-side logic here!!
Debug.LogFormat(" w");
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
DoSomethingServerSide(3);
}
}
so it will send it for the client 3 ,.thanks .
Your answer

Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
UNet - Connecting and Testing a 'choose your character' scenario 0 Answers
Photon Cloud Networking: OnPhotonSerializeView Not Firing 9 Answers
UNet Audio Help 1 Answer
Reducing server-side application size 0 Answers