Search Unity

Unity Multiplayer [SOLVED] NullRef in NetworkManager.OnServerAddPlayer()

Discussion in 'Multiplayer' started by Palimon, Jul 13, 2015.

  1. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    I'm not sure why it's throwing this error when I select Host from the Network Manager HUD. I've also tried using the default/provided NetworkManager class and have the same result. I recently changed my Player class significantly right before it started throwing this error - could it be that I'm not labelling something needed as a [SyncVar]? I'm pretty new to the Unity so I haven't a clue :/

    Error:
    This error is thrown on the final line of my OnServerAddPlayer method:
    Code (CSharp):
    1.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    2.     {
    3.         var player = (GameObject)GameObject.Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
    4.         GameObject parent = GameObject.Find("Hull");
    5.         player.transform.parent = parent.transform;
    6.  
    7.         Debug.LogWarning("NetworkServer: " + NetworkServer.active);
    8.         Debug.LogWarning("conn: " + conn);
    9.         Debug.LogWarning("player: " + player.name);
    10.         Debug.LogWarning("playerControllerId: " + playerControllerId);
    11.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    12.     }
    The debug statements print out:
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    does the playerPrefab have a NetworkIdentity? Also try upgrading to the latest patch release of Unity.
     
  3. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    I'm up to date on Unity, and the player prefab does have a NetworkIdentity. Here's screenshots of the player prefab and my network manager object:

    Player prefab.PNG Network Manager.PNG

    Any other ideas?
     
  4. peterept2

    peterept2

    Joined:
    Aug 1, 2012
    Posts:
    41
    Is it even possible to set non-sync var values during OnServerAddPlayer ??

    You are setting: player.transform.parent=parent.transform;

    But how would that get synced to remote clients??

    Maybe try commenting that out.
     
  5. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Got it. If I removed [SyncVar] from a variable in my Player class, then the error goes away. Is it expected behavior for a null SyncVar to blow up all networking functionality?
     
  6. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Well, I'm not sure. I'm about to find out :).
     
  7. peterept2

    peterept2

    Joined:
    Aug 1, 2012
    Posts:
    41
    No, I meant you are setting player.transform.parent which isn't a syncvar.
    Any time I set values on my player's prefab in OnServerAddPlayer() they have been syncvar so they get sent to it.

    Interesting removing a SyncVar actually stopped the error though...
     
  8. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Yeah, know what you meant - I had just already bumped into removing the SyncVar annotation. Didn't expect it to do anything, I was just down to commenting things out until it started working again.
     
  9. peterept2

    peterept2

    Joined:
    Aug 1, 2012
    Posts:
    41
    What was your syncvar. I didn't see it on your screenshot in Player_NetworkSetup.
     
  10. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Just a random variable on my player class:
    Player class.PNG
     
  11. peterept2

    peterept2

    Joined:
    Aug 1, 2012
    Posts:
    41
    "Controller" - that is the reason!

    You can only sync basic types (like int, string, Vector2, etc). You can't sync a reference to another object. The system does not know how to serialzie it into a byte stream.
     
  12. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Ahhhhh, thanks!
     
unityunity