Search Unity

NullReferenceException Player null

Discussion in 'Scripting' started by Hankok, Jan 6, 2020.

  1. Hankok

    Hankok

    Joined:
    Nov 30, 2019
    Posts:
    4
    Hi, i have one problem with Player null, when i try to create this:
    Code (CSharp):
    1.         private void DrawPlayers()
    2.         {
    3.             try
    4.             {
    5.                 foreach (var player in _ply)
    6.                 {
    7.                     if (player == null)
    8.                     {
    9.                         File.WriteAllText("error.txt", "player object is null.");
    10.                         File.WriteAllText("error.txt", _ply.ToString());
    11.                         foreach (var p in _ply)
    12.                         {
    13.                             File.WriteAllText("error.txt", $"Player {p.Profile?.Info?.Nickname ?? "'Unknown' is null"}: \n{p.ToString()}");
    14.                         }
    15.                         File.WriteAllText("error.txt", _ply.ToString());
    16.                         return;
    17.                     }
    Its droping me player null and dont now how to fix it.
    any idea? thank you
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So, where in your code are you actually setting your reference to your player in _ply? And how have you made sure you always do that before ever calling DrawPlayers?
     
  3. Hankok

    Hankok

    Joined:
    Nov 30, 2019
    Posts:
    4
    Its in assemblycsharp.dll am using Player reference all ready, am not sure, becauss of that am asking here for help. If am sure then it will not be problems.


    CODE:

    Code (CSharp):
    1.             if (_showPlayersInfo && Time.time >= _plyNextUpdateTime)
    2.             {
    3.                 _ply = FindObjectsOfType<Player>();
    4.                 _plyNextUpdateTime = Time.time + _infoUpdateInterval;
    5.             }
    Code (CSharp):
    1. namespace Absolutly
    2. {
    3.     public class Abso : MonoBehaviour
    4.     {
    5.         public Abso() { }
    6.  
    7.         private GameObject GameObjectHolder;
    8.  
    9.         private IEnumerable<Player> _ply;
     
  4. Hankok

    Hankok

    Joined:
    Nov 30, 2019
    Posts:
    4
    bump
     
  5. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Code (csharp):
    1. File.WriteAllText("error.txt", $"Player {p.Profile?.Info?.Nickname ?? "'Unknown' [URL='http://www.google.com/search?q=is+msdn.microsoft.com']is[/URL] null"}: \n{p.ToString()}");
    This line is the problem. You see above here, you check if player is null, and find that it is. Then, you iterate through the same list and try to access members (
    p.Profile
    ). That should always throw an exception.
     
  6. Hankok

    Hankok

    Joined:
    Nov 30, 2019
    Posts:
    4
    Hi, thx i try out that i and found its Camera Main null error. Here is code but idk how to fix that problem at all. If you can help me out thank you...
    Code (CSharp):
    1.      
    2.         private void DrawPlayers()
    3.         {
    4.             try
    5.             {
    6.                 foreach (Player player in _players.Where(plr => plr != null))
    7.                 {
    8.                     if (player == null)
    9.                     {
    10.                         File.WriteAllText("greske.txt", "Here we go with player null error.");
    11.                         return;
    12.                     }
    13.  
    14.                     if (player.Transform == null)
    15.                     {
    16.                         File.WriteAllText("greske.txt", "Player.Transform is null error");
    17.                         return;
    18.                     }
    19.                     if (Camera.main == null)
    20.                     {
    21.                         File.WriteAllText("greske.txt", "Camera.main is null error");
    22.                         return;
    23.                     }
    24.  
    25.                     float distanceToObject = Vector3.Distance(Camera.main.transform.position, player.Transform.position);
    26.                     Vector3 playerBoundingVector = new Vector3(Camera.main.WorldToScreenPoint(player.Transform.position).x, Camera.main.WorldToScreenPoint(player.Transform.position).y, Camera.main.WorldToScreenPoint(player.Transform.position).z);
    27.  
    28.                     if (distanceToObject <= _maxDrawingDistance && playerBoundingVector.z > 0.01 && playerBoundingVector.x > -5 && playerBoundingVector.y > -5 && playerBoundingVector.x < 1920 && playerBoundingVector.y < 1080)
    29.                     {
    30.                         var playerHeadVector = new Vector3(
    31.                             Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position).x,
    32.                             Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position).y,
    33.                             Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position).z);
    34.  
    35.                         float boxVectorX = Camera.main.WorldToScreenPoint(player.Transform.position).x;
    36.                         float boxVectorY = Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position).y + 10f;
    37.                         float boxHeight = Math.Abs(Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position).y - Camera.main.WorldToScreenPoint(player.Transform.position).y) + 10f;
    38.                         float boxWidth = boxHeight * 0.65f;
    39.  
    40.                         Color guiBackup = GUI.color;
    41.                         var playerColor = GetPlayerColor(player.Side);
    42.                         var isAi = player.Profile.Info.RegistrationDate <= 0;
    43.                         var deadcolor = player.HealthController.IsAlive ? playerColor : Color.gray;
    44.  
    45.                         GUI.color = deadcolor;
    46.                         GuiHelper.DrawBox(boxVectorX - boxWidth / 2f, (float)Screen.height - boxVectorY, boxWidth, boxHeight, deadcolor);
    47.                         GuiHelper.DrawLine(new Vector2(playerHeadVector.x - 2f, (float)Screen.height - playerHeadVector.y), new Vector2(playerHeadVector.x + 2f, (float)Screen.height - playerHeadVector.y), deadcolor);
    48.                         GuiHelper.DrawLine(new Vector2(playerHeadVector.x, (float)Screen.height - playerHeadVector.y - 2f), new Vector2(playerHeadVector.x, (float)Screen.height - playerHeadVector.y + 2f), deadcolor);
    49.  
    50.  
    51.                         var playerName = isAi ? "[BOT]" : player.Profile.Info.Nickname;
    52.                         string playerDisplayName = player.HealthController.IsAlive ? playerName : playerName + " [MRTAV]";
    53.                         string playerText = $"{playerDisplayName} [{(int)distanceToObject}]M";
    54.  
    55.                         var playerTextVector = GUI.skin.GetStyle(playerText).CalcSize(new GUIContent(playerText));
    56.                         GUI.Label(new Rect(playerBoundingVector.x - playerTextVector.x / 2f, (float)Screen.height - boxVectorY - 20f, 300f, 50f), playerText);
    57.                         GUI.color = guiBackup;
    58.                     }
    59.                 }
    60.             }
    61.             catch (Exception ex)
    62.             {
    63.                 File.WriteAllText("greske.txt", ex.ToString());
    64.             }
    65.         }
    66.  
    67.  
     
    Last edited: Jan 21, 2020