Search Unity

Unity gives incorrect code line for error - always end of function

Discussion in 'Editor & General Support' started by AussieSwissDave, Aug 7, 2020.

Thread Status:
Not open for further replies.
  1. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Hello,

    I recently started getting this program with Unity and VS Code. I'll get an error such as this:

    upload_2020-8-7_15-32-23.png

    Pretty standard stuff. The problem though is that line 606 in FixtureController is this:

    upload_2020-8-7_15-33-10.png

    And that's simply the closing bracket to the function. So basically Unity is telling me nothing more informative than "you have an argument out of range exception" in this function, whereas it used to give the actual line that causes the error.

    Any assistance would be much appreciated because debugging is very tough right now.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Does your cs file have mixed line endings maybe? Or is it possible that you have unsaved changes in your file?
     
  3. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Thank you for your reply. Not sure. I definitely didn't do anything deliberately. How would I check or fix this issue of mixed line endings?
     
  4. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    So I checked VS code for each of my .cs scripts and they are all set to "LF".
     
    PraetorBlue likes this.
  5. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    And I haven't gotten any inconsistent line endings warning that others have supposedly mentioned.

    The incorrect error line, always pointing to the end of the function, only applies to the final function. If you see below:

    upload_2020-8-8_3-24-12.png

    Line 54 of "FixtureController" is the end of the function, so that's my problem:

    upload_2020-8-8_3-26-42.png

    but line 125 refers correctly to the line of code calling TestFunction:

    upload_2020-8-8_3-26-31.png

    Same goes for lines 393 and 289 of the other files. So it's just the final bit that fails...

    Any help would be much appreciated. Nearly impossible to use Unity right now, unless with a load of Debug.Log statements :)
     
  6. Deleted User

    Deleted User

    Guest

    Could you post your entire scripts within code tags, please?
     
  7. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    So after a lot of unnecessary fiddling I finally got Unity to switch back from VS Code to Visual Studio. I can now confirm that the problem still persists with Visual Studio.

    Honestly no idea what to do now.
     
  8. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Here is a very simple example of what's going wrong. The error points to line 16, and not line 14 which is containing the error.

    upload_2020-8-8_11-56-0.png
     
  9. Deleted User

    Deleted User

    Guest

    File a bug report, it's the only way. :)
     
  10. Deleted User

    Deleted User

    Guest

    Out of curiosity, I reproduced your example in MonoDevelop and the error message in Unity points to line 17 instead of 16, the same way it does for you:
    Code (CSharp):
    1. ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    2. Parameter name: index
    3. System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <fb001e01371b4adca20013e0ac763896>:0)
    4. System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <fb001e01371b4adca20013e0ac763896>:0)
    5. Test.Start () (at Assets/Test.cs:17)
    Capture d’écran_2020-08-08_18-13-39.png
     
    Sinister-Design likes this.
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    By the way I very much doubt this has anything to do with your code editor. By the time the Unity game is running Unity has compiled the game all by itself.
     
  12. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Thank you very much folks for helping me out with this.

    I have done a complete reinstall of Unity (and including a slight update) and Visual Studio.

    @APSchmidt are you surprised by this?

    Here is another very simple test, after the reinstall:

    upload_2020-8-8_12-31-55.png

    Surely the bug should point to line 19, no? If I have a function with 100's of lines of code, how would I know where the ArgumentOutOfRangeException occured?
     
  13. Deleted User

    Deleted User

    Guest

    Not really, I've recently noticed an inconsistency in the error messages in Unity. The error specified that ";" was expected although the "=" assignment operator was missing in a line; and if I remember correctly, it also mentioned the wrong line number.
     
  14. Deleted User

    Deleted User

    Guest

    Line 19, yes. Maybe you could add other lines of code in your example and see what line number the error message points to?
     
  15. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Here we go @APSchmidt:

    upload_2020-8-8_12-50-22.png

    Out of those 3 loops, the first and third should work but the third should fail when ii = 3.

    The error points to line 44, i.e the ending curly brace, and one would have no idea a priori where the error occured.

    Are you able to reproduce this? Thank you again so much for your help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PleaseWork : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         List<int> testList1 = new List<int>();
    11.         List<int> testList2 = new List<int>();
    12.         List<int> testList3 = new List<int>();
    13.  
    14.         testList1.Add(0);
    15.         testList1.Add(1);
    16.         testList1.Add(2);
    17.  
    18.         testList2.Add(0);
    19.         testList2.Add(1);
    20.         testList2.Add(2);
    21.  
    22.         testList3.Add(0);
    23.         testList3.Add(1);
    24.         testList3.Add(2);
    25.  
    26.  
    27.         for (int ii = 0; ii <= 1; ii++)
    28.         {
    29.             testList1[ii]++;
    30.         }
    31.  
    32.         for (int ii = 0; ii <= 5; ii++)
    33.         {
    34.             testList2[ii]++;
    35.         }
    36.  
    37.         for (int ii = 0; ii <= 2; ii++)
    38.         {
    39.             testList3[ii]++;
    40.         }
    41.  
    42.  
    43.  
    44.     }
    45.  
    46.     // Update is called once per frame
    47.     void Update()
    48.     {
    49.      
    50.     }
    51. }
     
  16. Deleted User

    Deleted User

    Guest

    I meant after the faulty line, only one is enough. Try drowning it among many other correct Debug.Log lines and see what line the error message points at.
     
  17. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Debug.Log messages work, i.e. unity references the correct line of code (here line 36):

    upload_2020-8-8_12-59-22.png

    But the error reference still refers to the end of the function:

    upload_2020-8-8_13-0-1.png
     
  18. Deleted User

    Deleted User

    Guest

    I think you have enough material to report a bug. :)
     
    PraetorBlue likes this.
  19. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Oh I reported one a while back this morning.

    I just tried forcing another type of bug, a NullReferenceException, and this one does actually point to the right location:

    upload_2020-8-8_13-56-23.png

    Have you ever come across this issue with ArgumentOutOfRangeExceptions?

    Do you have any other suggestions? I've spent about 12 hours trying to fix this so far and I only have limited time time off my real job to work on it so it's been incredibly frustrating being unable to do so.

    Don't think there is anything more about Unity/VS that I can re-install, and this is a brand new project. My main project literally is 10's of thousands of lines long so I can hardly be swamping it with Debug.Log statements everywhere.
     
  20. Deleted User

    Deleted User

    Guest

    Not that I remember.
     
  21. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Okay well thank you very much for your help :)

    Hopefully the Unity bug report comes through. I tried upgrading to the latest Alpha version and it still fails.

    And if anyone else has some experience PLEASE let me know. I literally cannot use Unity right now.
     
  22. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    A little bump to see if anyone has ever come across this. Trying to plug away with my work but finding it pretty hard to do any debugging if ArgumentOutOfRangeExceptions purely point to the end of a function.
     
  23. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Does the actual debugger work properly? If you put in breakpoints or exception breakpoints in VS Code?
     
  24. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Thank you for your response.

    No, it does not do anything if I add a breakpoint in VS Code. I downloaded the "Unity Debugger" extension for it too.

    Other aspects of debugging do work though. If other types of errors come up, Unity will display the correct line of code and clicking on it will correctly open VS Code. It seems to just be ArgumentOutOfRangeExceptions which simply point to the end of the function.

    Note that this problem exists in Visual Studio and VS Code.
     
  25. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    I'll add that I don't use breakpoints so I don't know whether or not they used to work but upon just adding one, I can see it did nothing.

    I really just need the errors to point to the correct line. Everything used to be fine until a week ago and no idea why it stopped working properly *sigh*
     
  26. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    So above when I created a brand new project in Unity 2020 you can see there is still that problem.

    I just tried creating one in Unity 2019 and wow, things are back to normal and working again!

    This doesn't at all solve why Unity 2020 is misbehaving, but it's a start. However, unsurprisingly any attempts to roll my project back to Unity 2019 completely fail.

    Just tried deleting the Library in the game and having that all re-load but no luck.

    Any help would be greatly appreciated. Really all of my work has stopped dead in its tracks and I've probably spent about 12 hours so far trying to fix this.
     
    shinichikudo997 likes this.
  27. Deleted User

    Deleted User

    Guest

    Why rolling back when you can just get back to the latest version of your game you backed up before upgrading?
     
  28. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    I am now doing this. Unfortunately the version is about 2 weeks old. I believe all of these problems would have started with the upgrade from 2018 -> 2020, but for most of these past two weeks I have been doing stuff on the 3D graphics side and hence wouldn't have really noticed debugging problems.

    Unfortunately that 3D stuff I'll have to redo now for version 2018, but hopefully won't be more than say a day of work to merge everything together.

    Frustrating though that Unity would release a new version with such a breaking bug in it :(
     
  29. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    I'm curious though that you replicated my code and got the same error. What version of Unity are you using @APSchmidt?

    A cool new feature I was enjoying in 2020 (maybe it was in 2019, I don't know) was the debugging would highlight nested lines of code leading up to the error. Before they would be written but not hyperlinked in blue for easy access. Maybe in adding this feature though they screwed up the line determination of the final error.
     
  30. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    upload_2020-8-11_1-29-31.png

    Seeing that going back to 2018 seems to solve things as the correct line 17 is highlighted, not line 21 which would have been highlighted in 2020.

    Although clicking on the error doesn't seem to take you to it... but I mean that's a small issue compared to the error line being wrong.
     
  31. Deleted User

    Deleted User

    Guest

    2020.2.0a15.

    I just tried your first bit of code in 2019.2.21f1 and the error message in console points to the right line:

    Capture d’écran_2020-08-11_09-17-56.png

    Capture d’écran_2020-08-11_09-24-07.png
    (I'm sorry, the line number is barely visible but I don't know how to change that in MonoDevelop.)
     
    Last edited by a moderator: Aug 11, 2020
  32. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Very interesting. Is it possible that they just completely screwed things up with 2020? Surely not... I'm struggling to find anyone else complaining about this.
     
  33. Deleted User

    Deleted User

    Guest

    Does this happen in 2019.4?
     
  34. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Nope!

    I just created a new project in 2019.4.7 and the debugging works properly.

    I then upgraded it to 2020.1.1 and, without changing anything, the debugging stops behaving, pointing to the end of the function.

    And it seems like you're experiencing the same thing? Do you normally use 2020? If so, how come this hasn't irked you so far? It's definitely likely that I have functions that are too long and that if they were smaller it would be less of a problem to debug even with this issue, but still...
     
  35. Deleted User

    Deleted User

    Guest

    I use it every day for the Unity courses I'm following. So far the error messages I've gotten were predictable so I didn't really bother.

    Looks like I'm getting used to this kind of misbehaviour... :p
     
  36. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Maybe! I mean, I used it for 2 weeks before finally realizing there was a big problem, but most of that was spent doing stuff on the graphical side so I wasn't coding much.

    Oh well, hopefully when my Dropbox sync/rewind is finished in a few hours I can at least come back and say that works! And then of course need to do the arduous task of bringing this old version of the game up to date but at least the errors should work ;)

    Thank you so much for your assistance though!
     
  37. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    It's definitely possible that they broke something in the new version.
    Typically you need to also hit the "play" button as well to actually attach the debugger, in addition to adding a breakpoint.
     
  38. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    So restoring my 2018 version has "fixed" this problem. To me it's just 2020 being screwy. Disappointed to waste so much time on it and I hope the Unity Team can get this fixed so others don't have to as well.

    Will definitely be much more careful with any engine upgrades from now on!
     
  39. Deleted User

    Deleted User

    Guest

    Here is a script you can use to test the new versions:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using System;
    4. using System.Linq;
    5.  
    6. public class Test : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     private void Start()
    10.     {
    11.         Debug.Log("Hello, World!");
    12.         OutOfRangeTest();
    13.     }
    14.  
    15.     //test to make sure the space bar works as intended
    16.     //and if your keyboard is recognized by Unity (for non English keyboards)
    17.     private static readonly KeyCode[] KeyCodes = Enum.GetValues(typeof(KeyCode))
    18.         .Cast<KeyCode>()
    19.         .Where(k => ((int)k < (int)KeyCode.Mouse0))
    20.         .ToArray();
    21.  
    22.     // Update is called once per frame
    23.     private void Update()
    24.     {
    25.         if (Input.anyKeyDown)
    26.         {
    27.             for (int i = 0; i < KeyCodes.Length; i++)
    28.             {
    29.                 KeyCode kc = KeyCodes[i];
    30.                 if (Input.GetKeyDown(kc))
    31.                     Debug.Log($"Input detected: {kc.ToString()}");
    32.             }
    33.         }
    34.  
    35.         if (Input.GetKeyDown(KeyCode.Space))
    36.             Debug.Log("The space bar was pressed.");
    37.     }
    38.  
    39.     //test to make sure the script error messages
    40.     //point in the right direction
    41.     private void OutOfRangeTest()
    42.     {
    43.         List<int> testList = new List<int>();
    44.  
    45.         testList.Add(0);
    46.         testList.Add(1);
    47.         testList.Add(2);
    48.  
    49.         Debug.Log(testList[3]);
    50.     }
    51. }
    :p
     
  40. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Much appreciated! Definitely will do :)
     
  41. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    UPDATE: Unity got back to me and there is a simple fix to this:

    "Hi,

    Thanks for submitting your issue.

    The specific line of code is not provided for optimization reasons in the C# code Release mode. If you would like to get more detailed information about the exception, please use the Debug mode. It can be switched in the bottom-right corner of the Editor by clicking on the bug icon. I've attached a video demonstrating how to enable it and how it differs.

    I hope that helps. If you have any other questions, feel free to contact us again.

    Thanks,
    Mantas
    Customer QA Team"



    I guess this is a new feature to Unity 2020. Oh well, learn something every day :)
     
  42. Deleted User

    Deleted User

    Guest

    Did you check that it works?
     
    renanssba likes this.
  43. AussieSwissDave

    AussieSwissDave

    Joined:
    Apr 20, 2013
    Posts:
    97
    Yep! It does :)

    *I probably should have mentioned that clearly in my previous post.

    upload_2020-8-12_18-26-59.png

    Note that the error is correctly found at line 52, not 55 which would have been the case previously
     
    ZhavShaw and renanssba like this.
  44. Deleted User

    Deleted User

    Guest

    Sorry, I forgot to post here after I checked myself.

    I'm not sure how I feel about being forced to use the debug mode in the editor so that it's actually accurate.
     
  45. renanssba

    renanssba

    Joined:
    Feb 22, 2014
    Posts:
    7
    Thanks so much for this thread! I ran into this error today and was already trying to install an older version of Unity to be able to get accurate error messages. I'm glad they at least offered the option to show it by this debug mode
     
  46. foolmoron

    foolmoron

    Joined:
    Jun 22, 2013
    Posts:
    41
    Seconding the thanks for the this thread! I knew it had to be something really simple like the debug switch. Can you edit your original post with the solution to so it's easier for others to find?
     
  47. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,696
    The switch did it for me too. Wish there were a way in Unity to warn of this behavior outside of debug mode...
     
    Ruslank100 and Deleted User like this.
  48. impbox

    impbox

    Joined:
    Mar 31, 2017
    Posts:
    8
    I was able to get the correct line number for an ArgumentOutOfRangeException by enabling "Deep Profile" in the Profiler, this works even when not using the Profiler. This worked for me in Unity 2019.4 LTS.
     
    kimsungjinDC, lexen1 and palapapa93 like this.
  49. shinichikudo997

    shinichikudo997

    Joined:
    Jul 1, 2018
    Posts:
    33
    You just saved my day. :D
     
    Sinister-Design likes this.
  50. kimsungjinDC

    kimsungjinDC

    Joined:
    Jul 16, 2020
    Posts:
    5
    This absolutely works for me!!
     
Thread Status:
Not open for further replies.