- Home /
[Resolved] IL2CPP Argument Out Of Range Exception When using "in" keyword
I'm getting the following error when performing an Android build in Unity Cloud Build: [Unity] IL2CPP error for method 'System.Void VectorMath.Vector2D::.ctor(System.Single&,System.Single&)'
il2cpp.exe didn't catch exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
This error is referring to this code from my project:
public Vector2D(in float x = 0.0f, in float y = 0.0f)
{
this.x = x;
this.y = y;
}
I've been trying to make a faster Vector Math library for some more intensive operations. A lot of the methods take Advantage of the in
keyword introduced in C# version 7.2 to improve performance.
I'm currently using Unity 2018.4 LTS, and am using the following build settings:
Is this an issue with how I'm using the in
keyword, or is this an issue with compiler support? I read around and saw that the 2018.4 version should be able to support a .NET 4.6 Equivalent, which I've assumed up to this point means the .NET 4.x equivalent. Do I need to update to 2019.x to be able to use this? Source for C# compiler: https://docs.unity3d.com/2018.4/Documentation/Manual/CSharpCompiler.html
This is a bug in IL2CPP that has been corrected in 2019.4 and later, as far as I recall. If you would like to use the in
modifier, please try that version of Unity or newer.
Answer by andycodes · Aug 22, 2020 at 02:57 AM
So I found out what was failing, but I don't know why. Removing the in
keyword from the constructors that had default float values seems to have fixed all my woes. If anyone knows why this is, I'd like to be enlightened!
Working code:
public Vector2D(float x = 0.0f, float y = 0.0f)
{
this.x = x;
this.y = y;
}
Have you read the documentation? The in modifier makes the argument being passed by reference instead of passed by value. If there's an actual improvement is questionable since passing a float variable by reference would require a 64 bit memory address being passed in (at least on 64bit systems), instead of a 32 bit float value. Since the in modifier makes the argument passed by reference the parameter has to be a variable. In any case you don't pass in a variable but a constant / immediate value, the compiler has to generate a temp variable. Like Josh said above the IL2CPP compiler probably has / had issues with that required temp variable.