Search Unity

connectionToClient is null

Discussion in 'Multiplayer' started by psychicparrot, Jul 18, 2017.

  1. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    Hey guys!

    Could really use a little help with this Unity networking code.. (Unet in Unity 5.6.2)

    What it's supposed to do is:
    1. Client runs its first Update() and calls on the server to add a player (CmdAddPlayer).
    2. The server increases a number to use as a unique ID and then uses a TargetRpc function call (TargetIDCallback) to tell the client to spawn a player and to use the ID it passes back.
    3. The TargetRpc function sets a variable on the client side, to store the ID. It then calls on ClientScene to add the player to the game.

    Problem is that when I try to do the call to the TargetRpc function, connectionToClient is null. Even if I do GetComponent<NetworkIdentity>().connectionToClient I get null even though I'm connected successfully to the server.

    What could be causing connectionToClient to be null? Am I going at this the wrong way?

    Thanks for any / all the help :)
    Jeff.

    Here's my register / add player code so far:

    Code (CSharp):
    1.  
    2.    [ClientCallback]
    3.     void Update ()
    4.     {
    5.         if ( !didInit )
    6.         {
    7.             Debug.Log ("GameController.cs>Adding player..");
    8.             CmdAddPlayer ();
    9.             didInit = true;
    10.         }
    11.     }
    12.  
    13.     [Command]
    14.     public void CmdAddPlayer ()
    15.     {
    16.         uniqueIDs++;
    17.  
    18.         // tell the client to use this ID and create its player
    19.         TargetIDCallback ( GetComponent<NetworkIdentity>().connectionToClient, uniqueIDs);
    20.     }
    21.  
    22.     [TargetRpc]
    23.     void TargetIDCallback ( NetworkConnection target, short ID )
    24.     {
    25.         localPlayerID = ID;
    26.         ClientScene.AddPlayer (ID);
    27.     }


    This class derives from NetworkBehaviour and is attached as a component to an empty game object that also has a network identity component attached with neither server or client authority checked on it.​
     
    Last edited: Jul 18, 2017
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Don't quote me on it, but I think that and isLocalPlayer and similar vars are set AFTER the first frame. So you could try to make a coroutine and yield return null (Yields one frame) and then do what you need to do. Also, you don't need the GetComponent part. Just do connectionToClient. It's a public in NetworkBehaviour. Otherwise if it is the case that connectionToClient isn't set instantly. I guess you have to do a workaround. Such as yield to next frame as long as it's null or something.

    Also, please next time. Use the Forum code tags so it's easier for people to read.
     
    psychicparrot likes this.
  3. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    Thanks for the reply TwoTen. I'll give it a try this morning. I really appreciate the reply.

    Apologies for not using the tags on the code.. night turned to morning and my brain turned to mush! I'll use them next time :)
     
    Last edited: Jul 18, 2017
  4. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    Hmm. Still null. Always. I'm sure connectionToClient must work in some circumstances but I've no idea why it isn't working here. I looked around online and didn't find any example code using it, so it's super tough to figure out what could be different between working code and my non-working code.
     
  5. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Seems fairly odd. Is the object placed on a actual player object that has Authority? And are you a host or a client?
     
  6. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    This class derives from NetworkBehaviour and is attached as a component to an empty game object that also has a network identity component attached with neither server or client authority checked on it.

    Didn't read that...
    You have to specify what client. Find the player object. And do playerObject.GetComponent<NetworkIdentity>().connectionToClient.

    It doesn't just know what client you want to access out of the blue.
    As Stated in the docs:
    The connection associated with this NetworkIdentity. This is only valid for player objects on the server.
     
    psychicparrot likes this.
  7. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    Argh! Of course! connectionToClient only works for player objects and I haven't created the player yet. I dunno what I was thinking! I think I can fix this now thanks for the idea. I'll reply again here with an update later.

    Thanks, TwoTen. For whatever reason I got into the mindset that I needed TargetRpc to call the client back from its own script. Urgh. I should have just gone to bed and not tried to stay up and keep working!!
     
    TwoTen likes this.