Click here to Skip to main content
15,887,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, again sry for asking milion times about my project but i need to fix this issue its about one month now and am have still problem with Camera Main causing me black screen after some time.

<pre>        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);

                    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());
            }
        }


What I have tried:

- Debug no error at all. (Also its .dll file)
- Try catch and finde out its camera.main problem idk why.
- try to cache
- try to acces over FindObject
Posted
Updated 22-Jan-20 2:27am

If Camera.main is null, then just loading it into the debugger (And it doesn't matter if it's a dll or exe, the debugger will work fine) then just finding that out doesn't help you - the problem isn't in that code, it's in the code that executed some time before that code.

If you get a null, then you need to find out why: and that means using the debugger to trace though your code to find out exactly what it is doing: it isn;t "magic software" that men you load it immediately goes "There is a problem with your logic here". The debugger is a tool that lets you examine what your code is doing while it runs, it doesn't show you errors, it shows you what is going on so you can spot what code isn't doing what you expected it to.
So saying "Debug no error at all. (Also its .dll file)" means you aren't using it properly. And saying "Try catch and finde out its camera.main problem idk why." is also not helpful - as you need to look at what should be setting it to something other than null to find out what that either isn't being executed, or is loading the wrong value.

And i'm sorry, but we can't do any of that for you: we don't have your code, we don't have the data it uses, we don't know how to get it to where the problem occurs, and we don't even know what you expect it to do!
So load you app in the debugger, and start using it as a tool instead of going "it don't work" - only you can do that!
 
Share this answer
 
Comments
MRHals Gospodin 21-Jan-20 2:25am    
Am attached to process from game with break points but its hiting and nothing more... what now?
OriginalGriff 21-Jan-20 2:47am    
If it hits the breakpoints that's the whole idea! You are then in charge and can look at your variables, single step your code, change which line executes next, even edit it and continue from there.

As I said, 'it isn't "magic software" that when you load it immediately goes "There is a problem with your logic here". The debugger is a tool that lets you examine what your code is doing while it runs'
It lets you find out what is going on, and the interpretation of that is up to you - the debugger has no more idea of what your whole app is trying to do (much less the tiny code fragment you show) than I do!
It's like your car stopping on a motorway - the engine died, which is why the car stopped (Camera.Main is null). But what happened to cause the engine to die? Did it run out of fuel? Did you fill it with the wrong type of fuel? Did you fill the fuel tank with molasses? Did it overheat? Did it run into a brick wall in a very unlikely place? Did that engine warning light that came on a week ago have something to do with it? Debugging is the process of finding out what caused the problem you have noticed - and just like the "stopped car" you have to look back from the "fault point" to work out what lead up to the problem becoming notecable.

So find out where it should be set to a value, and breakpoint that. Does it get hit? If so, then what is it setting? If not, then why not?
Debugging is a skill, and you only develop it by using it. Think about your code, and what path(s) it followed to get to the point where it shows a problem.
MRHals Gospodin 21-Jan-20 13:32pm    
Am not expert with debug can you give me some hints, or at least you try to debug my program am can send you stuff that you try to find line whats i causing my problems over 1 month now..
OriginalGriff 21-Jan-20 13:59pm    
No - I can't debug your program for you - read what I said above!

Google for "Visual Studio" and "Debugger" and you will find lots of material on how to use it. The actual use and "knowing where to look and what to look at" depends on knowing your code - and you wrote it so you should know it and how it is supposed to work - plus knowing when something is not what it should be.

Give it a try. It's a skill - and one you really should have pretty much developed long before now ... you won't get far without it!
If your camera is becoming Null, Unity itself should be complaining in its error log, and not require the try-catch thing you have going on there. I don't know that I trust the results myself...
With that, I find interesting,
Quote:
black screen after some time
Does the camera work, and then not? If it is working initially, what conditions are leading to it eventually being nullified? Does your code have access to that camera?
There are a range of questions and possibilities to consider, and the sample code isn't particularly helpful.

What is helpful is doing what Griff suggested, and utilize the debugger. If you don't understand how, (we've all been there) take some time to figure it out. By doing so and gathering the results, you'll either:
A) Figure it out yourself, and learn along the way.
B) Give the people you seek aid from information to better help them help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900