Unity WebGL Build Crashes in Browser - How to Fix (Cross-Platform Issues)
Problem: Your Unity WebGL build loads in the browser but then crashes, freezes, or shows a blank or gray screen. The game may run in the Editor but fails when played in Chrome, Firefox, Safari, or Edge.
Quick Solution: Reduce memory usage, disable or replace unsupported threading, and test in multiple browsers. Adjust Player Settings for WebGL (memory limit, compression, code stripping) and fix any code that uses threads or blocking calls.
This guide walks you through the most common causes and fixes so you can get your WebGL build running reliably across browsers.
Why WebGL Builds Crash in the Browser
WebGL runs in a single-threaded, sandboxed environment. Crashes and freezes usually come from:
- Exceeding the WebGL memory limit (browser and Unity cap total heap size).
- Using threads or blocking APIs that are not supported in WebGL (e.g.
Thread.Sleep,System.Threading). - Heavy allocations or leaks causing the heap to run out.
- Shader or graphics API differences between the Editor and the browser.
- Browser-specific bugs or missing features (e.g. Safari, older mobile browsers).
- Large build size or assets leading to long load times or timeouts that look like crashes.
Fixing these in order of impact (memory, threading, then compatibility) usually resolves the issue.
Solution 1: Increase Memory and Optimize Player Settings
Unity’s WebGL player has a default memory limit. If your game needs more, or if you have not tuned for WebGL, the build can run out of memory and crash.
Step-by-Step Fix
-
Open Player Settings
- Go to Edit > Project Settings > Player.
- Select the WebGL tab (not Standalone).
-
Set Memory Size
- Under Publishing Settings, find Memory Size (or Initial Memory Size depending on version).
- Increase it in steps (e.g. 256 MB, then 512 MB). Do not set it arbitrarily high; 512 MB–1 GB is often enough. Very high values can cause allocation failures on some devices.
- Click Apply.
-
Enable Compression
- In Publishing Settings, set Compression Format to Gzip or Brotli if available.
- This reduces download size and can help with load time and stability on slow connections.
-
Code Stripping
- Under Other Settings, set Managed Stripping Level to Low or Medium to reduce build size. Use High only if you have verified the build still works; aggressive stripping can remove code the game needs and cause runtime errors.
-
Rebuild and Test
- Build for WebGL again and test in a browser. If it still crashes, try lowering memory slightly and reducing quality or content to see if the crash is memory-related.
Verification: The game loads past the progress bar and reaches the first scene without the tab crashing or showing a gray/blank screen.
Solution 2: Remove or Replace Threading and Blocking Calls
WebGL does not support multi-threading or blocking the main thread. Code that uses Thread, Thread.Sleep, or blocking I/O can cause freezes or crashes in the browser.
Step-by-Step Fix
-
Find Threading and Blocking Code
- Search the project for
System.Threading,Thread.Sleep,Task.Run(with blocking work), or synchronousUnityWebRequest(e.g.SendWebRequest()followed by a busy wait). - Use conditional compilation so this code does not run in WebGL:
- Wrap in
#if !UNITY_WEBGL || UNITY_EDITORor checkApplication.platform == RuntimePlatform.WebGLPlayerand skip or replace the logic.
- Wrap in
- Search the project for
-
Use Async Alternatives
- Replace
Thread.Sleepwithawait Task.Delay()or coroutines. - Use
UnityWebRequestasynchronously withawaitor callbacks instead of blocking. - Move heavy work to coroutines or split across frames so the main thread is not blocked.
- Replace
-
Avoid Unsafe Code
- WebGL has limited support for unsafe code and pointers. If you use
unsafeor pointers, guard them with#if !UNITY_WEBGLor provide a WebGL-safe path.
- WebGL has limited support for unsafe code and pointers. If you use
-
Rebuild and Test
- Build again and test. If the game no longer freezes on load or during play, threading/blocking was likely the cause.
Verification: The WebGL build runs without freezing; loading and gameplay proceed without long freezes or “Page Unresponsive” dialogs.
Solution 3: Reduce Allocations and Avoid Leaks
Large or repeated allocations can fragment the heap and push the build over the memory limit, especially in long sessions.
Step-by-Step Fix
-
Profile in the Editor
- Use the Profiler (Window > Analysis > Profiler) and Memory Profiler to find large allocations and leaks.
- Look for allocations in
Update, in frequently called methods, or in instantiation/destruction loops.
-
Reuse Objects
- Use object pools for frequently spawned objects (bullets, effects, UI elements) instead of instantiating and destroying them every time.
- Cache component and transform references instead of using
GetComponentorFindevery frame.
-
Reduce Texture and Mesh Usage
- Lower texture resolutions and use compressed formats where appropriate for WebGL.
- Simplify or LOD meshes so the build uses less GPU and CPU memory.
-
Rebuild and Test
- After optimizations, rebuild and run the WebGL build again. Monitor the browser’s memory (e.g. Chrome Task Manager) to see if usage stays stable over time.
Verification: Memory usage in the browser stays relatively stable during play; the tab does not crash after a few minutes.
Solution 4: Test and Fix Browser-Specific Issues
Some crashes only happen in certain browsers or on certain devices.
Step-by-Step Fix
-
Test in Multiple Browsers
- Test in Chrome, Firefox, Edge, and Safari (desktop and iOS if you target mobile).
- Note which browser and OS show the crash (e.g. “crashes only in Safari 17”).
-
Check Browser Console
- Open Developer Tools (F12) and check the Console for errors (e.g. WebGL context lost, out of memory, script errors).
- Check the Network tab for failed or very large asset loads.
-
Handle WebGL Context Lost
- Implement
Application.onBeforeRenderor listen for WebGL context loss if your Unity version exposes it. Save state and show a “Please refresh” message instead of leaving a broken canvas. - Avoid operations that are known to trigger context loss (e.g. too many draw calls or very large textures on low-end devices).
- Implement
-
Adjust Quality for Problem Browsers
- Lower default quality settings or detect the browser and reduce settings for known problematic configurations so the game can at least run.
Verification: The build runs in all browsers you support, or you have a clear message or fallback for unsupported ones.
Prevention Tips
- Design for WebGL early: If you plan a WebGL build, avoid threading and blocking APIs from the start; use async and coroutines.
- Set a memory budget: Pick a target memory size (e.g. 256–512 MB) and profile against it so you do not exceed browser limits.
- Test WebGL regularly: Build and test WebGL as part of your usual testing, not only at the end of the project.
- Document supported browsers: State which browsers and versions you support so users know what to expect.
When It Still Crashes
- Gray or blank screen: Usually memory, threading, or a fatal script error. Check the browser console and reduce memory usage and threading as above.
- Crashes after a few minutes: Often a memory leak or unbounded allocation. Profile and fix leaks and heavy allocations.
- Crashes only in one browser: Focus on that browser’s console and try lowering quality or disabling features for that build variant.
- Build never loads: Check for very large files, failed CDN or server config (e.g. MIME types for .data/.wasm), and that the loader and paths are correct.
Related Help and Guides
- Unity Build Fails with Out of Memory Error - How to Fix for general build memory issues.
- Unity Build Size Too Large - Optimization Solutions to reduce WebGL download size.
- Web Game Deployment Issues - Complete Solution Guide for hosting and deployment.
For more on Unity deployment, see the Building and Publishing Your Game section of our Unity guide.
Bookmark this fix for quick reference when working on WebGL builds. If this article helped you, share it with other developers who are shipping to the web.