Search Unity

How do you fix your code/find the problem?

Discussion in 'Getting Started' started by Oskar_Kasprzak, Jul 24, 2017.

  1. Oskar_Kasprzak

    Oskar_Kasprzak

    Joined:
    Mar 26, 2016
    Posts:
    61
    Hello there.

    Correct me if I am wrong, but I guess that asking/searching for an answer to your question isn't always perfect (not even mentioning even further steps where it's not easy to find someone with same problem). I just wonder how do you find what's wrong in your code? I am at very early step of developing and doing really small projects, so I am mostly using debug.log.

    Would love to hear how you're doing it on your way, especially from people who are working on "bigger" procjets. Maybe some "step by step" if something isn't working, or maybe some assets from Unity which allow you to develop faster?

    P.S I am creating this thread after fightning over an hour to solve a minor (yet big to me) problem.

    Best regards.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I rely heavily on Debug.Log myself when I'm not sure what's going on. I do this mainly because MonoDevelop is such a pig; after getting used to Script Inspector 3's instant loading & lightning-fast editing, MonoDevelop is really painful to use.

    However, if I'm really stuck and need to step through the code, then I will of course fire up MonoDevelop and set a breakpoint. Google "Unity debug with monodevelop" and I'm sure you'll find some explanations/tutorials.

    In more general terms, your code should be organized into units small enough that you can test them independently. Make your scripts as simple and independent as possible, so you don't have too much to keep in mind at once, and so that when something goes wrong, you don't have very much code to comb through to find the problem.

    Finally, you may eventually be writing logic complex enough to benefit from test-driven development (google that, too). Unfortunately you can't use test-driven development with typical Unity components — it really only works for deeper logic-level stuff, or for cases where you've made a clear separation between game logic and presentation. As a beginner, you probably don't need to worry about this — just tuck it away as something to look into again a few years down the road.
     
    Ryiah and Kiwasi like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    So the first step is to define what's wrong in your code. Code problems fall into several general categories
    • Compiler errors - The red line in the console and you can't enter playmode. These are errors in your actual syntax
    • Runtime errors - The red line in the console while you are in playmode. These are errors in your code.
    • Bugs - No errors, but the code doesn't behave as intended. These are errors in your logic.
    • Performance - No errors, everything behaves as intended. But its too slow.
    Compiler errors are the easiest to solve. You simply go to the line number of the first listed error. Fix the syntax. Recompile. Repeat.

    Runtime errors are next. Often the actual problem is a fair distance from the line number specified in the error. The error isn't always reproducible. There are two basic approaches to runtime errors, the first is to isolate the source of the problem and fix it so it never happens again. This is where Debug.Log, stepping through code, and breakpoints becomes super useful. The other strategy is to guard against the error, either by checking for it, or catching it when it happens. This approach is useful when you don't have control of all the code in your project.

    Bugs are the hardest to find. First need to catch the bug. Then you need to reproduce the bug. Then you need to identify what the bug actually is. Then you need to isolate the section of code in which the bug occurs. Then you need to step through the logic to find what is causing the bug. Then you need to fix it. This is hard work, there is a reason why even AAA games ship with the occasional bug.

    Performance is the last one to tackle. Step one is to profile it. Then work from high level optimizations down to low level optimizations. Ask questions like these in this order: Do I need to do this at all? --> Do I need to do it every frame? --> Do I need to do it for every entity? --> Is there a faster algorithm to do it? --> Can I make this specific line of code faster?. Often you will find that the problem will be solved well before you get to the end of this process.
     
  4. mihirTheCoder

    mihirTheCoder

    Joined:
    Jul 13, 2018
    Posts:
    9
    There's Unity Forums for a reason! Just search up your error on any search engine, and many answers will show up :)
     
  5. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    If you're using Debug.Log because you're not using the debugger, I highly recommend using the debugger in Visual Studio.

    Debugging things that happen every frame is more difficult, i recommend learning some of the Debug.Draw functions like Debug.DrawLine helps when doing vector math and you need to visualise each step of your calculation to make sure it's calculating correctly.

    It also pays to step through your code the first time you run it using the debugger. There are often simple little mistakes that you can catch doing this.


    Edit:
    One last thing. Learn the correct/industry standard terminology for common bugs in computer software, graphics and video games. That will help you google what's going wrong and ways to fix it.
     
  6. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Any suggestions on finding errors when this is what Unity gives you?

    upload_2020-8-10_19-24-53.png

    Great.... really helpful Unity telling me that there is an Argument Out Of Range Exception somewhere in a 1000+ function.

    Shouldn't Unity be a bit more descriptive than this and actually point to the line causing the problem?
     
    Last edited: Aug 11, 2020
  7. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Error message is pointing to FixtureController.cs, line 1185.
    Although having a 1000+ line long function is a problem in itself.

    A large function suggests it's performing multiple different things already, and a 1000+ line function is pretty much god-object territory.
    You want to keep your functions small and make them serve one purpose & one purpose only. It becomes much easier to debug and maintain then.
     
    Last edited: Aug 11, 2020
  8. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Thank you very much for your reply.

    The error is being caused by a loop extending out of bounds in a list. What happens though is that instead of Unity highlighting that particular loop (on some line sub-1000) as the cause of the error, it always just highlights the end bracket of the containing function. Isn't this a problem with Unity?

    I swear it didn't used to do this... @Vryken is this how you would expect Unity to behave?

    R.e. the programming style, I do agree with you and it's something I'm trying to get better at. However some of my code is what it is and it would be nice for the debugging to work properly.
     
  9. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    No, it shouldn't be doing that. From what I could find online, some users who had this issue suggested to navigate to:
    Edit > Preferences > External Tools and remove/re-add the script editor you're using.
    https://github.com/Unity-Technologies/vscode-unity-debug/issues/52

    These posts are from 2017-2018 though. I couldn't find anything more recent. Still, maybe worth a shot?
    If that doesn't work, perhaps try reinstalling Unity.
     
  10. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Thank you very much for looking into this.

    I had actually come across these posts before without much luck (perhaps since as you mentioned they're a bit old now). I tried their suggestions of removing then re-adding, and also changing the External Script Args, with no luck.

    Other things I have tried without success;

    1) Switch from VS code to Visual Studio... and then back as well
    2) Create an entirely new project with the simplest of codes to reproduce the problem, and it still occurs:

    3) Using the Unity Hub I have removed/reinstalled/upgraded Unity a few times, all to no luck.
    4) Reported a bug to Unity but no response as of now (admittedly just a couple of days)

    Is there perhaps some more "master" re-install that completely gets rid of Unity and all associated things?
     
  11. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Not that I'm aware of, unfortunately.
     
  12. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Okay well I'm rolling back to 2 weeks ago when I made the switch in my project from 2018 -> 2020. Hopefully this fixes it, although of course there will be some work needed to add back in my recent changes.

    Can confirm that Unity 2018 does not have this problem :)
     
    Last edited: Aug 11, 2020