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

NetworkManager.Singleton.LocalClient for finding the local player

Discussion in 'Netcode for GameObjects' started by cerestorm, Nov 12, 2021.

  1. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    I'm looking for the simplest method for finding the local player and NetworkManager.Singleton.LocalClient seems to fit the bill. It works fine on the client but returns null for the host, is this by design?
     
    codeBatt likes this.
  2. Khrisx

    Khrisx

    Joined:
    Jan 21, 2018
    Posts:
    1
    Maybe: NetworkManager.Singleton.ServerClientId ?
     
  3. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    I did look at that, the problem is it's the id and not the the NetworkClient that's returned by NetworkManager.Singleton.LocalClient. I can then find the the NetworkClient from connected clients, but why the extra work for the host and not the client, not to mention an extra conditional check for IsHost, yet IsClient is true for both host and client. Maybe it's just a bug.
     
  4. Talkyn

    Talkyn

    Joined:
    Apr 13, 2019
    Posts:
    8
    IsLocalPlayer is what I use, but I'm not sure if you can access it within the namespace NetworkManager.Singleton.WhatNot. I use it via inheritance from NetworkBehaviour rather than MonoBehaviour.

    As for IsClient returning true for the host, it is true. That is what a host is, a client and a server rolled into one.
     
    Last edited: Nov 13, 2021
  5. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    IsLocalPlayer looks a good alternative, I'll grab the player when it spawns and store the local player in the GameManager.

    The host also being a client was the point I was trying to make. The host is also from their perspective the local client so NetworkManager.Singleton.LocalClient should work.
     
  6. Talkyn

    Talkyn

    Joined:
    Apr 13, 2019
    Posts:
    8
    Ah I get it now. I'd wager there is a reason it is behaving like the sever and not like it is also a client. Now I'm curious, so I'm going to take a look at it. I'll let you know if I learn anything.
     
    cerestorm likes this.
  7. Talkyn

    Talkyn

    Joined:
    Apr 13, 2019
    Posts:
    8
  8. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    Thanks for looking, pity you can't trawl through the code and search in Visual Studio directly, I'm too lazy to download it. :D

    I'll stick with using IsLocalPlayer, it was the way I used previously, just as you rightly surmised I was trying to get the local player from within a MonoBehaviour.
     
  9. Talkyn

    Talkyn

    Joined:
    Apr 13, 2019
    Posts:
    8
    I scanned through all the issues and I don't see this listed. I suppose a good citizen should put the work in and create a good issue. I'll copy the repo--handy for looking things up anyway--and put this on my todo for tonight or tomorrow.
     
    cerestorm likes this.
  10. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    I'd hope Unity would be actively looking out for these issues on the forum to save us the trouble. But thanks for looking in it. :)
     
  11. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Usually I look at all Forum threads, but was on vacation last week. The simplest way to find the local player that I know of would be: NetworkManager.Singleton.SpawnManager.GetLocalPlayerObject();
     
    seemantadebdas and cerestorm like this.
  12. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    656
    That's a great tip, thanks.