Hi cansomebody give me "hint" / "point" where is my misstake in code that i recive camera.main null error? Thank you for time and respon and pls sory for my english, am not perfect if you can explain little bit of easy way that i can understand will aprichiate very much, its c# script and i cant debug it becauss its .dll i try to attach to process with breakpoints but nothing giving me at all.
C#:
private void DrawPlayers()
{
try
{
foreach (Player player in _players.Where(plr => plr != null))
{
if (player == null)
{
File.WriteAllText("greske.txt", "Here we go with player null error.");
return;
}
if (player.Transform == null)
{
File.WriteAllText("greske.txt", "Player.Transform is null error");
return;
}
if (Camera.main == null)
{
File.WriteAllText("greske.txt", "Camera.main is null error");
return;
}
float distanceToObject = Vector3.Distance(Camera.main.transform.position, player.Transform.position);
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);
if (distanceToObject <= _maxDrawingDistance && playerBoundingVector.z > 0.01 && playerBoundingVector.x > -5 && playerBoundingVector.y > -5 && playerBoundingVector.x < 1920 && playerBoundingVector.y < 1080)
{
var playerHeadVector = new Vector3(
Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position).x,
Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position).y,
Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position).z);
float boxVectorX = Camera.main.WorldToScreenPoint(player.Transform.position).x;
float boxVectorY = Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position).y + 10f;
float boxHeight = Math.Abs(Camera.main.WorldToScreenPoint(player.PlayerBones.Head.position).y - Camera.main.WorldToScreenPoint(player.Transform.position).y) + 10f;
float boxWidth = boxHeight * 0.65f;
Color guiBackup = GUI.color;
var playerColor = GetPlayerColor(player.Side);
var isAi = player.Profile.Info.RegistrationDate <= 0;
var deadcolor = player.HealthController.IsAlive ? playerColor : Color.gray;
GUI.color = deadcolor;
GuiHelper.DrawBox(boxVectorX - boxWidth / 2f, (float)Screen.height - boxVectorY, boxWidth, boxHeight, deadcolor);
GuiHelper.DrawLine(new Vector2(playerHeadVector.x - 2f, (float)Screen.height - playerHeadVector.y), new Vector2(playerHeadVector.x + 2f, (float)Screen.height - playerHeadVector.y), deadcolor);
GuiHelper.DrawLine(new Vector2(playerHeadVector.x, (float)Screen.height - playerHeadVector.y - 2f), new Vector2(playerHeadVector.x, (float)Screen.height - playerHeadVector.y + 2f), deadcolor);
var playerName = isAi ? "[BOT]" : player.Profile.Info.Nickname;
string playerDisplayName = player.HealthController.IsAlive ? playerName : playerName + " [MRTAV]";
string playerText = $"{playerDisplayName} [{(int)distanceToObject}]M";
var playerTextVector = GUI.skin.GetStyle(playerText).CalcSize(new GUIContent(playerText));
GUI.Label(new Rect(playerBoundingVector.x - playerTextVector.x / 2f, (float)Screen.height - boxVectorY - 20f, 300f, 50f), playerText);
GUI.color = guiBackup;
}
}
}
catch (Exception ex)
{
File.WriteAllText("greske.txt", ex.ToString());
}
}