Search Unity

Unity Multiplayer Get local player's game object

Discussion in 'Connected Games' started by erebel55, Jun 10, 2015.

  1. erebel55

    erebel55

    Joined:
    Jun 14, 2014
    Posts:
    42
    The new multiplayer API exposes a isLocalPlayer boolean which is useful for checking if the script is being ran on the local player.

    Is there anything in the API to get the local player's game object?

    I see PlayerController in the docs which has a reference to the gameobject, but I don't know how to retrieve this component.

    I also see playerControllerId on various components, but no example for retrieving the PlayerController with this.

    I know that I can name the local player's game object and retrieve it using Find or even better tag the local player's game object and retrieve it using FindGameObjectWithTag.

    But is there a better way through the Networking api?

    Thanks
     
    klesun likes this.
  2. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    318
    I like to use the override, OnStartLocalPlayer, which will only be called on NetworkBehaviours on the local player object (and then cache a reference to the gameObject as needed).
     
    christophermrau likes this.
  3. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    There is a list of players in the NetworkConnection object - there could be multiple players on a client.

    On a client, it is in NetworkManager.client.connection.playerControllers - this is only players on this client. Not all the players in the game.
     
  4. Alexees

    Alexees

    Joined:
    Nov 8, 2017
    Posts:
    181
    singleton accessor missing:
    NetworkManager.singleton.client.connection.playerControllers[0].gameobject
     
  5. Orion_78

    Orion_78

    Joined:
    Feb 13, 2014
    Posts:
    66
    Did you drag & drop a NetworkManager into your scene ?
    Do you use your own NetworkManager and have made your own Awake function ? That kill the singleton too
     
  6. Orion_78

    Orion_78

    Joined:
    Feb 13, 2014
    Posts:
    66
    How can you get all player's game Object ? Do we have to make our own list ?

    From client side, is there a way to access theses player with a synchronized id ? Or do you have to make our own list/id ?



    Tanks
     
  7. Alexees

    Alexees

    Joined:
    Nov 8, 2017
    Posts:
    181
    I wasn't talking about an error I received, but about a correction from seanr's code line, which does not work because it lacks the singleton call.
     
  8. hallura

    hallura

    Joined:
    Dec 9, 2017
    Posts:
    7
    I'd like to know this, too. Did you figure it out?
     
  9. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    624
    Does ClientScene.localPlayers() do what you need?

    You can also use the netId propetry of NetworkBehavior and ClientScene.FindLocalObject(NetworkInstanceId netId)
     
    elich11 and alaeddin like this.
  10. elich11

    elich11

    Joined:
    Sep 25, 2012
    Posts:
    4
    Thanks.
    This:

    public override void OnStartLocalPlayer()
    {
    print("OnStartLocalPlayer");
    GameObject player = ClientScene.FindLocalObject(netId);
    print ("player y: " + player.transform.position.y);
    }

    worked.