Question Unity Camera main returns null

MrHalls

New member
Joined
Jan 21, 2020
Messages
4
Programming Experience
3-5
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());

        }

    }
 
Line 35, you are checking for null but on line 47, you are trying to access what could potentially throw a null exception. Same again on line 59. The blocks of code between them should reside in the else section of your if null checks. Your method is also a little on the long side, and really should be broken up to abide by OOP rules.

And Unity - Scripting API: Camera.main <= more or less says that if you spawn your player without the camera being attached, and enabled, and both your player and camera are initialised, you will not get that error. Why did you compile into a DLL?
 
Line 35, you are checking for null but on line 47, you are trying to access what could potentially throw a null exception
Almost... recall that if Camera.main is null on line 35, then line 41 will exit out of the function before getting to line 47. Same is true if player.Transform is null on line 25, then line 31 will exit out of the function before getting to line 47. Now what could be null, though is Camera.main.transform.
 
Line 35, you are checking for null but on line 47, you are trying to access what could potentially throw a null exception. Same again on line 59. The blocks of code between them should reside in the else section of your if null checks. Your method is also a little on the long side, and really should be broken up to abide by OOP rules.

And Unity - Scripting API: Camera.main <= more or less says that if you spawn your player without the camera being attached, and enabled, and both your player and camera are initialised, you will not get that error. Why did you compile into a DLL?
Mono internal 3 party softwar :D
 
Almost... recall that if Camera.main is null on line 35, then line 41 will exit out of the function before getting to line 47. Same is true if player.Transform is null on line 25, then line 31 will exit out of the function before getting to line 47. Now what could be null, though is Camera.main.transform.
Thank you :)
 
When i first read it, I didn't see any return. Never-mind me, I'm still a bet jet lagged from travelling. :oops: Skydiver is right. Similarly to why exec() is unreachable in pseudo sample because of using return :
C#:
            if (true)
                return null;
            exec();
A second glance at line 59, would encourage me to just make sure your player is spawned and the camera is enabled and both are initialised before trying to access the camera. You also want to make sure that the camera is actually set as your main camera. (y)
 
When i first read it, I didn't see any return. Never-mind me, I'm still a bet jet lagged from travelling. :oops: Skydiver is right. Similarly to why exec() is unreachable in pseudo sample because of using return :
C#:
            if (true)
                return null;
            exec();
A second glance at line 59, would encourage me to just make sure your player is spawned and the camera is enabled and both are initialised before trying to access the camera. You also want to make sure that the camera is actually set as your main camera. (y)
thank you :) very much
 

Latest posts

Back
Top Bottom