- Home /
[UNET] OnPlayerConnected is never called
Hi. I have a script with only one method OnPlayerConnected attached to a game object in online scene but the method is never called and there is no Debug.Log output neither on server nor on client. Any ideas?
void OnPlayerConnected(NetworkPlayer player)
{
Debug.Log("Player " + playerCount + " connected from " + player.ipAddress + ":" + player.port);
}
Yes, it is NetworkBehaviour. Does it matter? NetworkBehaviour derives from $$anonymous$$onoBehaviour.
Answer by UNino123 · Jul 30, 2016 at 04:33 PM
As seanr said, its not going to be called on UNET. What I generally do, is have a class that controls all the network messages (simple commands and rpc, connections and disconnections), which, in server is always scanning the NetworkManager.numPlayers; This way, if someone connects or disconnects the number of players will change and you can decide what to do...
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class NetworkMSG : NetworkBehaviour {
NetworkManager NetMngr;
NetworkClient meClient;
public int connectedPlayers=0;
void Start(){
NetMngr = GameObject.Find ("NetworkManager").GetComponent<NetworkManager> ();
if (isServer) {
NetworkServer.RegisterHandler (MsgType.Highest + 2, OnMessageRX); // if you are a server, subscribe to the message handler
GameObject.FindGameObjectWithTag ("Player").GetComponent<NetPlayerController> ().playerNum = 1;
GameObject.FindGameObjectWithTag ("Player").GetComponent<NetPlayerController> ().playerRegistered = true;
}
if(isClient){
meClient = NetMngr.client; // if you are client save your network client (to send messages to server through the handler)
}
}
//CLIENT SCRIPTS
[ClientRpc]
void RPCClient(string Garden)
{
if (!isClient)
return;
}
//Client send command to server
public void ClientSendTree(string treeData){
if (!isClient)
return;
TreeMessage Tmessage= new TreeMessage();
Tmessage.tree = treeData;
//if (!isServer) {
meClient.Send (MsgType.Highest + 2, Tmessage);
Debug.Log ("Just registred this message on network client");
//}
}
//SERVER SCRIPTS
public void Update(){
if (!isServer)
return;
if (NetMngr.numPlayers != connectedPlayers) { //detect player connection
if (NetMngr.numPlayers < 2) {
connectedPlayers = NetMngr.numPlayers;
}
if(NetMngr.numPlayers >connectedPlayers){
Debug.Log ("Player connected");
StartCoroutine(PlayerConnected (3));//wait five seconds before sending garden
connectedPlayers = NetMngr.numPlayers;
}
Debug.Log ("NetworkMSG: Players connected to host= "+(connectedPlayers-1).ToString());
RegisterClients();
}
}
IEnumerator PlayerConnected(float waitTime){
yield return new WaitForSeconds(waitTime);
Debug.Log ("NetworkMSG: Waited for client to connect, to send garden info for: " + waitTime);
TransmitGarden ();
}
public void Reset(){
if (!isServer)
return;
}
void OnMessageRX(NetworkMessage netmsg){ //receive from user
Debug.Log ("Message Received");
if (!isServer)
return;
TreeMessage msg = netmsg.ReadMessage<TreeMessage> ();//get message
}
}
public class TreeMessage : MessageBase
{
public string tree; // in the format zntm (zone, numberOfTree, TreeType, Method)
}
This is the majority of what you would use for a network communication protocol for your basic game, (player connect, client setup, event handler, network messages, RCP calls, etc) ... Hope it is of some use to anyone starting on this. ( I have just started myself, but this code I wrote has worked so far, in various different games I made).
Answer by seanr · Jan 15, 2016 at 03:42 PM
OnPlayerConnected is from the legacy network API, not UNET.
How can i find out if a player connected to the server using UNET?
The way I did it was I made a class where the player sends a command to change a Boolean on another class. I then just check if the Boolean has been set to true. I couldnt find any other way but for my needs it works 100% of the time. Hope this helps!
Well, i can also count the players using Find GameObjects by Tag "Player" to know if the count has changed. But i want UNET to do it for me.Its really buggy. Photon is light years better than UNET for now.
How great that it is till there in documentation. Classic Unity.
Your answer
![](https://koobas.hobune.stream/wayback/20220612053834im_/https://answers.unity.com/themes/thub/images/avi.jpg)