Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Unity Multiplayer How to find the spawned Player gameobject?

Discussion in 'Multiplayer' started by Glabrezu, Jun 28, 2015.

  1. Glabrezu

    Glabrezu

    Joined:
    Aug 30, 2014
    Posts:
    24
    On the client side, how do I find the spawned gameobject that belongs to the proper client?

    My understanding was that all Players' gameobjects could be retrieved through ClientScene.localPlayers, which contains a list of PlayerControllers. Each PlayerController has a playerControllerId, which is a short.

    The question is then, I suppose, how do I find my playerControllerId on the client?
     
  2. darkmask58

    darkmask58

    Joined:
    May 9, 2015
    Posts:
    25
    Sorry if I'm wrong but first you set the client to be Ready ClientScene.Ready(conn) , then you pass the controller id on the ClientScene.AddPlayer(id), so you can find the player by its id on the list ClientScene.localPlayers[id].
     
  3. Glabrezu

    Glabrezu

    Joined:
    Aug 30, 2014
    Posts:
    24
    I'm not sure you answered my question.
    And what's more, OnClientConnect is the one that calls ClientScene.AddPlayer and the parameter that it passes is always 0.

    So basically I don't understand the logic of it all, and my question of how to find the correct Player gameobject for the local client is still unanswered.

    Another thing that I tried was to run this code on a class of the gameobject that servers as the PlayerPrefab, but it didn't work either:
    Code (csharp):
    1.  
    2.   public override void OnStartClient()
    3.   {
    4.     base.OnStartClient();
    5.  
    6.     if (isLocalPlayer)
    7.     {
    8.       // I assumed "this" would be the gameobject that I'm looking for, but isLocalPlayer does   something else apparently
    9.     }
    10.   }
    11.  
     
  4. Glabrezu

    Glabrezu

    Joined:
    Aug 30, 2014
    Posts:
    24
    By running the same code in a simple Start(), it worked.
    No idea why OnStartClient() didn't yield the same results.
     
  5. darkmask58

    darkmask58

    Joined:
    May 9, 2015
    Posts:
    25
    Maybe OnStartClient is executed before you can have a localplayer.