WebGL build fails with IL2CPP Error

I have a Unity project that when building to WebGL produces the following error:


Error: IL2CPP error for type 'System.Net.WebHeaderCollection/RfcChar' in assembly 'C:\PathToProject\Library\Bee\artifacts\WebGL\ManagedStripped\System.dll'  

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

This only happens with one project. I see that there are other WebGL and IL2CPP issues posted, but they’re all different and none of them seem to have answers.

Any thoughts on how to solve this issue?

I have the same Issue. My project works building for ios, but when trying to build for webgl I get the error:

Error: IL2CPP error for type 'System.Net.WebHeaderCollection/RfcChar' in assembly '/Users/jasonraiti/project/Library/Bee/artifacts/WebGL/ManagedStripped/System.dll'
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

same here with unity 2021.3.5f1, trying to update project from 2021.1

I had the same error, the project was building to WebGL fine, then stopped. Not sure what changed.

I removed parts of the project until it worked.

Turned out this bit caused it to fail to build to WebGL (ran within Unity fine)

Dictionary<string, string> requestBody = new Dictionary<string, string>()
        {
            {"state", "COMPLETED" }
        };
        // Serialize body as a Json string
        string requestBodyString = JsonConvert.SerializeObject(requestBody);

This relied on

using System.Collections.Generic;
using Newtonsoft.Json;

I’m guessing it was something to do with Newtonsoft.Json, not sure of the best way to handle JSON in Unity so I just swapped out that section for a string to avoid needing that library.

seems this compilation bug is only in 2021.3 LTS, at least 2021.3.5 i revert to 2021.2.19 and i don’t have the bug anymore, so i’ll stick to that.

i can also confirm i’m using newtonsoft json like @jonjhiggins above.

EDIT bug reported Jira Service Management

EDIT 2 : bug acknowledged : Unity Issue Tracker - Error &quot;IL2CPP error for type &#39;System.Net.WebHeaderCollection/RfcChar&#39;&quot; is thrown when building on WebGL platform
but they said it’s fixed from 2021.3.6f1, which is not …

same issue here with 2021.3.6f1 LTS. I’m also using Newtonsoft Json

The only solution, for now, is to downgrade Unity (I have 2021.3.1f1) and then use “JSON .NET For Unity” package instead.

If you’re going to use this package, I strongly advise you to:

  • AOT Assembly set to ANY platform with the exclusion of Editor
  • Standalone Assembly set to Editor platform only
  • remove all other Assemblies of this package

No changes are needed in the code.

With the new version 2022.1.10f1 it works,With the new Version 2022.1.10f1 it works.