Search Unity

Scripts with isLocalPlayer as child game object always return false

Discussion in 'Multiplayer' started by Manato, Dec 7, 2015.

  1. Manato

    Manato

    Joined:
    Jun 11, 2015
    Posts:
    28
    Hi,
    I'm testing the new networking and have some problems.
    I have a movement script that checks for "isLocalPlayer".
    If I attache this script on the root game object it works fine, but if I attache it in a child game object no movement will happen.

    Why is it so and how can I solve it?
     
  2. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    isLocalPlayer is only true (or false for that matter) for the GameObject that has the NetworkIdentity. This means that if GO (GameObject) B is a child of GO A, and A has the NetworkIdentity component, only GO A will return true for isLocalPlayer. Eve though B is a child of A, any script on B checking for isLocalPlayer will return false. You can use this for a work-around...

    Code (CSharp):
    1. gameObject.transform.parent.gameObject.GetComponent<NetworkIdentity>().isLocalPlayer
     
    PloyMetha and alsharefeeee like this.
  3. Manato

    Manato

    Joined:
    Jun 11, 2015
    Posts:
    28
    Thanks, it works fine.

    But its a bit strange, because I have a network identity on my child too.
    Anyway thanks for help.
     
  4. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Glad to help, but just to explain a bit more. When the NetworkManager spawns the player GO, it sets the isLocalPlayer to true, but only on the base GO. If you want any children to also have their "isLocalPlayer" set to true, you'll have to write your own override for that function.
     
  5. drsalvation

    drsalvation

    Joined:
    May 2, 2014
    Posts:
    48
    I know this is old, but the workaround can be simplified to transform.parent.GetComponent<NetworkIdentity>().isLocalPlayer (no need to call for gameObject). This has worked perfectly amazing for me, thanks!
     
    alsharefeeee and Joe-Censored like this.