- Home /
OnServerAddPlayer() is not called
I added NetworkManager and add my player (PlayerCube) on the NetworkManager.
Although the server and client connected well with the HUD, the OnServerAddPlayer() function is not called.
I found it is not called in Client side, but it is not called Server side, either. (There's no change on GUI, to display 'tmp')
The class MyNetManagerScript is my custom script for the NetworkManager, as shown below.
Can anyone advise me how to solve this problem?
public class MyNetManagerScript : NetworkManager {
string tmp = " ";
void OnGUI()
{
// ME HERE; is printed on the screen of course, but...
GUILayout.Label("ME HERE; " + tmp);
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader)
{ // IS THIS CALLED? IF NOT, WHY NETWORK PLAYER CAN BE MOVED BY KEY?
Debug.Log("A Player Connected"); // NOT PRINTED in the console
tmp = "ON PLAYER"; // NOT SHOWN on the screen
OnServerAddPlayer(conn, playerControllerId, extraMessageReader);
}
}
I am having this exact same problem and would love to hear the answer as well.
I find the Unity documentation extremely lacking in details for many things like this.
If there are limitations or requirements to how to envoke OnServerAddPlayer() it is certainly not being explained in the docs.
I have the same problem as well. There is an issue in the issue tracker that says the method isn't called when auto create players is on, but the OnServerAddPlayer is not called for me even if I disable that option and call ClientScene.AddPlayer... I don't know what to do...
Answer by andrewzimmer · Dec 02, 2016 at 06:30 PM
I was struggling with this same problem and eventually fixed it by changing the method description. There are 2 versions of OnServerAddPlayer
This one
public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader)
And this one
public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId);
For whatever reason, the second one with only two variables is what worked for me.
For reference, I also UNCHECKED the Auto Create Players option on the Network Manager, and added the OnClientConnect call.
Here's my function overrides in a custom subclass of Network Manager
public override void OnClientConnect(NetworkConnection conn) {
ClientScene.AddPlayer(conn, 0);
}
public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId) {
print ("Player Added to Server Yo");
base.OnServerAddPlayer (conn, playerControllerId);
}
Had the same issue in V 2017.1.0p4 and using the two-parameter overload made it work.
Thank you!
Answer by markgrossnickle · Dec 14, 2017 at 07:27 PM
I ran across a similar issue in that my OnServerAddPlayer was never called. It turns out it was because I added my own custom handler on the client for MsgType.Connect. I didn't realize RegisterHandler replaced other handlers and I assumed incorrectly it just added its own.
Leaving this here in case someone else stumbles on the same assumption.
Answer by 11Buff · Oct 10, 2016 at 08:39 AM
Just a tip, not needed but it's a habit. Don't do:
string tmp = " ";
Instead
string tmp;
This is my preference. Just saying. Maybe you need it to be " ";
string tmp; will create an uninitialized variable resulting in a null reference error should anything attempt to use it. By adding the = ""; it becomes an empty string, or in the above case a single space that can be used. They are quite different things. If you are certain you will populate the string before ever using it then you don't need to initialize it, but in his case he's using it on OnGUI, so the code would not work if he did not initialize it.