Quick answer: RuntimeError: memory access out of bounds is a WebAssembly trap: compiled code read or wrote an address outside the heap. In a Unity Web build the usual cause is a C# null reference or bad array index that was compiled without checks. Set Enable Exceptions to Full With Stacktrace in a development build to get the real exception, then check .jslib string buffers, heap limits and mixed build files.
The build works in the Editor, then the browser tab stops with a dialog and one line in the console that names no script and no line number. That line comes from the WebAssembly engine, which only knows that an address was invalid. The work is getting from that address back to a line of C# or a line of plugin JavaScript. This guide lists the causes in the order they turn up in real projects, shows the two settings that make the stack readable, and gives a correct .jslib string pattern next to the versions that corrupt the heap.
- The exact error
- What causes it
- How to find which cause is yours
- Fix 1: turn the trap back into a C# exception
- Fix 2: correct the .jslib plugin
- Fix 3: stay under the heap limit
- Fix 4: stop serving mixed builds
- Fix 5: unsafe code, threads and engine version
- How to confirm the fix
- Getting the stack from players’ browsers
- Related guides
- Frequently asked questions
The exact error
Unity’s loader shows an alert over the canvas, and the same text is in the browser console:
An error occurred running the Unity content on this page. See your browser JavaScript console for more info. The error was:
RuntimeError: memory access out of bounds
The wording depends on the browser, because each engine has its own message for the same trap:
RuntimeError: memory access out of bounds Chrome, Edge and other Chromium browsers
RuntimeError: index out of bounds Firefox
RuntimeError: Out of bounds memory access Safari
A Unity Web build is one WebAssembly module with one block of linear memory, the heap. Every load and store is checked against the current size of that block. When compiled code uses an address past the end, the engine stops the module and throws a RuntimeError. Nothing inside the build can catch it, the main loop does not run again, and the player has to reload the page. It happens in builds only: the Editor runs your scripts on Mono with full checks and would have shown a normal exception.
The alert is the generic one Unity shows for any uncaught error. The other errors that arrive in it are covered in the guide to the Unity content error dialog.
What causes it
- A C# null reference or out-of-range index compiled without checks. According to Unity’s Web Player settings documentation, null reference and array bounds checks are only embedded in the code when Enable Exceptions is Full Without Stacktrace or Full With Stacktrace. On None, and on the default Explicitly Thrown Exceptions Only, a field read through a null reference or
items[items.Length]is a raw memory access. Sometimes it reads garbage and carries on. Sometimes it traps. This is the most common cause by a wide margin. - A
.jslibplugin that writes past a buffer or hands back a bad pointer. A string copied into a buffer sized withstr.length, a missing byte for the null terminator, a pointer that was never allocated with_malloc, or a C# string pointer kept after the call returned. - The heap hit its limit. An allocation fails, the caller does not check the result, and the next write goes to an invalid address. Large
Texture2DandAudioCliploads, big asset bundles and mobile browsers are where this shows up. - Files from two different builds served together. A cached
.framework.jsor.datafrom last week with today’s.wasm. The glue code and the module disagree about offsets, and the first call that depends on them traps, usually during startup. - Unsafe or native code. Pointers,
stackalloc,NativeArrayindexing (its bounds checks exist only in the Editor), a native C/C++ plugin compiled into the build, or the experimental Enable Native C/C++ Multithreading option. - An engine bug. Rare, and the last thing to conclude. It is real often enough that testing on the newest patch release of your Unity line belongs on the list.
How to find which cause is yours
Start by making the stack readable. In a release build the console shows frames like these, which tell you nothing:
RuntimeError: memory access out of bounds
at wasm://wasm/0a53f1b6:wasm-function[38512]:0x9d2a41
at wasm://wasm/0a53f1b6:wasm-function[38507]:0x9d1f03
Two settings change that:
- Enable Development Build in the build window. Development builds are not minified and keep function names.
- In Edit > Project Settings > Player > Publishing Settings, set Debug Symbols to Embedded (or External for a release build, which writes a separate symbols file).
With symbols, the frames carry IL2CPP names, which follow the pattern ClassName_MethodName_m plus a hash. The top frame that belongs to your code is where to look:
RuntimeError: memory access out of bounds
at InventoryUI_Refresh_m3C1A9F0E2B (wasm://wasm/...)
at InventoryUI_OnEnable_m77D10B4C55 (wasm://wasm/...)
Then read the stack for these patterns:
- Your own class at the top: cause 1. Confirm it with the exceptions setting in the next section.
- A JavaScript function from your plugin, or
_malloc,_freeordlmallocnear the top: cause 2. A crash inside the allocator means memory was corrupted earlier by someone else, and plugins are the usual someone. - Texture, audio or asset bundle loading functions, and a heap near its maximum: cause 3. Turn on Show Diagnostic Overlay in the Publishing Settings to watch WASM heap use in the running build.
- It fails before the first scene on some machines and never on a fresh profile: cause 4.
If the stack points at nothing recognisable, bisect. Remove .jslib and native plugins one at a time (stub out the C# calls), and load scenes individually. A trap that moves around between runs is almost always earlier heap corruption, which points back at plugin or unsafe code.
Fix 1: turn the trap back into a C# exception
Open Edit > Project Settings > Player > Publishing Settings and find Enable Exceptions. In Unity 6 it sits under the WebAssembly Language Features heading; in 2022 LTS it is at the top of the Publishing Settings. The options are:
| Option | What a null reference or bad index does |
|---|---|
| None | Unchecked memory access. Any exception that is thrown stops the content. |
| Explicitly Thrown Exceptions Only (default) | Unchecked memory access. Only throw statements produce exceptions. |
| Full Without Stacktrace | Throws NullReferenceException or IndexOutOfRangeException. |
| Full With Stacktrace | The same, with a managed stack trace in the console. |
Build a development build with Full With Stacktrace and reproduce the crash. If cause 1 is yours, the trap is replaced by an ordinary log entry:
NullReferenceException: Object reference not set to an instance of an object.
at InventoryUI.Refresh () [0x00000] in <00000000000000000000000000000000>:0
Now it is a normal bug. Typical WebGL-only versions of it: a field filled by a coroutine or web request that has not finished yet, a Resources.Load or Addressables result that is null because the asset was stripped from the build, a singleton whose Awake ran in a different order than in the Editor, and reflection or JSON code that lost a type to managed code stripping.
// Broken: slots is filled by a web request that may not have completed
void Refresh()
{
for (int i = 0; i <= slots.Length; i++) // null in a fresh session, and off by one
icons[i].sprite = slots[i].icon;
}
// Fixed
void Refresh()
{
if (slots == null || icons == null) return;
int count = Mathf.Min(slots.Length, icons.Length);
for (int i = 0; i < count; i++)
icons[i].sprite = slots[i].icon;
}
Accesses that Unity itself validates, such as using a destroyed GameObject, throw explicitly and are logged on the default setting too. Plain C# objects and arrays are the ones that go unchecked.
Full With Stacktrace makes the build slower and larger, and Unity recommends it for debugging only. Fix the bug, then ship on the default. Shipping on Full Without Stacktrace is a reasonable trade if you would rather have a logged exception than a dead tab.
Fix 2: correct the .jslib plugin
Strings cross the boundary as pointers to UTF-8 bytes in the heap. To return one to C#, allocate with _malloc, size it with lengthBytesUTF8 plus one byte for the terminator, and pass that same size to stringToUTF8:
mergeInto(LibraryManager.library, {
GetPlayerName: function () {
var name = localStorage.getItem("player_name") || "";
var size = lengthBytesUTF8(name) + 1;
var buffer = _malloc(size);
stringToUTF8(name, buffer, size);
return buffer;
}
});
[DllImport("__Internal")]
private static extern string GetPlayerName();
When the pointer is the function’s string return value, the IL2CPP runtime frees it for you. The versions that break:
// Wrong: length counts UTF-16 units, so "Zoë" or "プレイヤー" needs more bytes than this
var buffer = _malloc(name.length);
stringToUTF8(name, buffer, 256); // a "big enough" limit lets the write run past the allocation
// Wrong: returns a JavaScript string where C# expects a pointer
return name;
// Wrong: keeps a pointer that is only valid during the call
SetTitle: function (strPtr) {
setTimeout(function () { document.title = UTF8ToString(strPtr); }, 1000);
}
The first one works in every test with an ASCII name and corrupts the allocator’s bookkeeping for the first player with an accent in theirs. The crash then happens later, inside some unrelated malloc. The third reads memory that C# released as soon as the call returned; copy it immediately with var title = UTF8ToString(strPtr); and use the copy in the callback.
Arrays follow the same rule. A float[] argument arrives as a pointer that is valid for the duration of the call, read with HEAPF32[(ptr >> 2) + i]. Do not store HEAPU8, HEAPF32 or their .buffer in your own variable either. When the heap grows, the old ArrayBuffer is detached and Emscripten replaces the global views, so a saved copy reads as empty and its writes are lost. Use the globals at the moment you need them. Unity’s browser scripting documentation has the reference versions of these patterns.
Fix 3: stay under the heap limit
The memory settings are in the same Publishing Settings panel in Unity 2022 LTS and Unity 6:
- Initial Memory Size: 32 MB by default.
- Memory Growth Mode: None, Linear or Geometric (recommended). On None the initial size is also the maximum, which is a quick way to produce this error.
- Maximum Memory Size: 2048 MB by default. Unity 6 accepts up to 4096 MB, and its documentation warns of known Firefox and Chrome bugs above 2048 MB.
Raising the maximum does not help on phones. Mobile browsers give a tab far less than desktop ones, and iOS Safari is the strictest. The fix there is using less: lower texture import sizes for the Web platform, set long audio clips to streaming or compressed in memory, load content in smaller bundles and unload what you are finished with. The Unity WebGL out-of-memory guide goes through each of those.
Fix 4: stop serving mixed builds
Enable Name Files As Hashes in the Publishing Settings. Each build file is then named after an MD5 hash of its contents, so a new build has new URLs and a browser or CDN cannot pair an old file with a new one. If you keep fixed names, upload the whole Build folder as one unit, purge the CDN after every deploy, and never cache index.html for long. To check whether a player is affected, ask them to hard-reload (Ctrl+Shift+R) or open the game in a private window.
Fix 5: unsafe code, threads and engine version
Audit anything that skips the managed safety net: unsafe blocks, stackalloc sized from input, NativeArray and UnsafeUtility calls, and native plugins compiled into the build. Out-of-range indexing on a NativeArray is reported in the Editor and silent in a player, so run the same code path in the Editor with the same data first. Leave Enable Native C/C++ Multithreading off unless you are deliberately testing it; Unity labels it experimental.
When a clean project with no plugins still traps inside engine functions, update to the latest patch release of your LTS line and rebuild before spending more time. If it persists, a small reproduction project and the symbolicated stack are what a Unity bug report needs.
How to confirm the fix
- Rebuild as a release build with your shipping exception setting and Debug Symbols set to External, so the next report is readable.
- Test in Chrome, Firefox and Safari. They lay out memory differently, and a write that lands harmlessly in one traps in another.
- For a plugin fix, test with non-ASCII input: a player name with accents, CJK text, an emoji.
- For a memory fix, play the heaviest scene for several minutes with the diagnostic overlay open, then repeat on a phone.
- Deploy to the real host and load it once from a browser that had the old build cached.
Getting the stack from players’ browsers
A trap stops the module, so no C# code runs afterwards and the Unity SDK cannot report it from inside the build. Two things still reach you. If you ship with exceptions set to Full Without Stacktrace, the bug is logged as an exception, and Bugnet’s Unity SDK files every logged exception with its stack trace, recent log lines, platform and game version. For the raw trap, load the Web SDK on the page that hosts the build: its window error listener receives the RuntimeError and files it with the JavaScript stack, the last 50 console lines, a screenshot of the canvas and the user agent. Keep the external symbols file for each release so those frames can be read. Bugnet does not symbolicate them for you. Crash reporting for Unity WebGL builds covers the setup.
Related guides
- How to Fix “An error occurred running the Unity content on this page” in Unity WebGL
- How to Fix a Unity WebGL Build Running Out of Memory
- Crash Reporting for Unity WebGL Builds
- Fix: Unity WebGL Build Crashing on Safari iOS
- How to Fix “Unable to parse Build/WebGL.framework.js.gz” in a Unity WebGL Build
Frequently asked questions
What does RuntimeError: memory access out of bounds mean in Unity WebGL?
It is a WebAssembly trap. Compiled code tried to read or write an address outside the build’s heap, so the browser stopped the module. In Unity it usually comes from a C# null reference or bad array index compiled without checks, a .jslib buffer mistake, or a heap that ran out.
Why does my game work in the Unity Editor but crash with this error in the browser?
The Editor runs scripts on Mono, which checks every null reference and array index and throws an exception. A Web build compiled with the default exception setting leaves those checks out, so the same bug becomes a raw memory access that the browser traps.
How do I get a readable stack trace for a Unity WebGL crash?
Make a development build, set Debug Symbols to Embedded in the Publishing Settings, and set Enable Exceptions to Full With Stacktrace. The console then shows IL2CPP function names and, for script bugs, a normal C# exception with the class and method.
Is index out of bounds in Firefox the same error?
Yes. Firefox reports the trap as RuntimeError: index out of bounds, Safari as RuntimeError: Out of bounds memory access, and Chromium browsers as RuntimeError: memory access out of bounds. The cause and the fixes are the same in all three.
Does increasing Maximum Memory Size fix memory access out of bounds?
Only when the heap limit is the cause. The default maximum is 2048 MB and Unity 6 allows 4096 MB with known browser bugs above 2048. Phones give a tab much less than either, so reducing texture, audio and bundle memory is the fix that works everywhere.
The browser only reports an address. Build once with Full With Stacktrace and embedded debug symbols before changing anything else, because that single build usually tells you whether you are fixing a script, a plugin or a memory limit.