NetworkConnection.SendToClient not working for server?

Okay its pretty simple, i want to send the server my id and password when i log in, and obviously i do not want to make a rpc where everyone can see it but only the server can. So I did a pretty simple message and I did it to -1 as the id of the client, and i also tried 0, but neither worked with retrieving or sending the message.

Notes : I start the server with a NetworkingManager, there is no prefabs other than the networking manager in the scene, and connections are taking place but when i print the count of all clients, the number does not increase after a connection has tooken place.

TempMsg msg = new TempMsg();
string temp = “hello”;
msg.msgData = System.Text.Encoding.UTF8.GetBytes(temp);

//print (NetworkClient.allClients.Count);

SendToClient(0,MsgType.Highest + 5, msg);

public void OnTempMsg(NetworkMessage netMsg)
{
TempMsg tempMsg = netMsg.ReadMessage();
byte[ ] recievedBytes = tempMsg.msgData;

NetworkServer methods only work on the host. Clients are only aware of the host and not any other clients.

Easiest way to do what you want is to spawn a networked player prefab that is just an empty with a NetworkBehaviour derivative on it. Then send a Command from that script. This will execute on the host’s version only.

Server communication is working like a charm now since i use command, so i did what you said and i made a empty prefab with this code and a network identity, but the server cannot talk to the client now, even with the correct connection id… which is strange :confused:

So this is what I did just to get a communication going was this, but for some reason this is always failing, the server is responding with the correct id…

Weird thing is, when i do start the server, the server retrieves the message, but none of the clients do…

public void Start(){

NetworkManager.singleton.client.RegisterHandler(MsgType.Highest + 5, OnTempMessage);
CmdSendMyInfo(“hello”);

}

… (code from above)

[Command]
public void CmdSendMyInfo(string whatInfo)
{

TempMsg msg = new TempMsg();
string temp = “hello”;

msg.msgData = System.Text.Encoding.UTF8.GetBytes(temp);

NetworkServer.SendToClient(transform.GetComponent().connectionToClient.connectionId, MsgType.Highest + 5, msg);

}

I’ve found connectionToClient/Server to be flaky from time to time. I’d try just caching the connection itself in OnConnectedToServer

Or use connectionToClient.SendByChannel https://docs.unity3d.com/ScriptReference/Networking.NetworkConnection.SendByChannel.html