Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[UNET] Detecting when a player has connected

Discussion in 'UNet' started by Murgilod, Jun 18, 2017.

  1. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,084
    I'm trying to make it so that when a new player connects to the server, they're added to a list. I know how to add things to a list, that's trivial. What I don't know how to do is get a new player connection with Unet. How do I do this exactly?
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Easiest way is to have a script on the player object with a OnDisable method.
     
  3. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,084
    So there's no really simple solution like an "OnClientConnect" or "OnClientDisconnect" that gets called on the host when they connect or disconnect for any reason?
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    OnDisable does that. It runs whenever the object is disabled due to disconnect. Works very reliably. If you are disabling the object during runtime yoursef this will cause an issue. Instead use OnDestroy then. But for all Network Callbacks, here they are:
    https://docs.unity3d.com/ScriptReference/Networking.NetworkBehaviour.html
     
  5. Deleted User

    Deleted User

    Guest

    if you are do networking using unet NetworkManager you could override OnServerConnect, "if(connectiodId !=0) will ignore host itself"

    Code (CSharp):
    1. protected int currentPlayers;
    2.  
    3.     public override void OnServerConnect(NetworkConnection conn)
    4.     {
    5.         if (conn.connectionId != 0)
    6.         {
    7.             currentPlayers += 1;
    8.         }
    9.     }