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

Hello,

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

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

6180972--676992--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.

4 Likes

Does your cs file have mixed line endings maybe? Or is it possible that you have unsaved changes in your file?

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?

So I checked VS code for each of my .cs scripts and they are all set to “LF”.

1 Like

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:

Line 54 of “FixtureController” is the end of the function, so that’s my problem:

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

6182222--677201--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 :slight_smile:

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

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.

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.

File a bug report, it’s the only way. :slight_smile:

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:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <fb001e01371b4adca20013e0ac763896>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <fb001e01371b4adca20013e0ac763896>:0)
Test.Start () (at Assets/Test.cs:17)

6183047--677339--Capture d’écran_2020-08-08_18-13-39.png

1 Like

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.

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.

@ are you surprised by this?

Here is another very simple test, after the reinstall:

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?

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.

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

Here we go @ :

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PleaseWork : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        List<int> testList1 = new List<int>();
        List<int> testList2 = new List<int>();
        List<int> testList3 = new List<int>();

        testList1.Add(0);
        testList1.Add(1);
        testList1.Add(2);

        testList2.Add(0);
        testList2.Add(1);
        testList2.Add(2);

        testList3.Add(0);
        testList3.Add(1);
        testList3.Add(2);


        for (int ii = 0; ii <= 1; ii++)
        {
            testList1[ii]++;
        }

        for (int ii = 0; ii <= 5; ii++)
        {
            testList2[ii]++;
        }

        for (int ii = 0; ii <= 2; ii++)
        {
            testList3[ii]++;
        }



    }

    // Update is called once per frame
    void Update()
    {
     
    }
}

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.

Debug.Log messages work, i.e. unity references the correct line of code (here line 36):

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

I think you have enough material to report a bug. :slight_smile:

1 Like

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:

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.

Not that I remember.