Hello to everybody,
so im facing this error “Runtime Error: memory access out of bounds.” when i run my webGL build on some mobile browsers.
These are my tests:
Fail: Chrome for android, version
87.0.4280.101 (latest stable until now);
Success: Firefox for android, version
84.1.1 (latest stable until now);
Fail: Safari for iOS (latest stable
version);
Fail: Chrome for iOS (latest stable
version);
Fail: Firefox for iOS (latest stable
version);
On desktop, works correctly on any
browser.
How do i generate this error:
Just by running a WWW or UnityWebRequest to download a file data like this:
byte[] data = null;
WWW www = new WWW(url);
yield return www;
if (www.error != null)
throw new Exception(www.error);
else
data = www.bytes;
print(www.text.Length);//Printing the text lenght give the error
print(data.Length);//Printing the bytes lenght doesnt give the error
Note: it happens only with files that are like 10/20MB or heavier, i’ve tried with a file that is less than 1MB and it works correctly on all the mobile browsers.
I already read that a solution should be by increasing the memory allocation / memory heap, but i didnt understand how.
But nothing changes, the error is still showing.
And yes, i tried even with UnityWebRequest but still same error.
Ps: Actually i dont need to print the text lenght, but since i need to access to that string, the error will be given anyway even just for a simple access to the string (i guess its because the string is too large and so i should allocate memory first, BUT HOW?)
I’ve finally fixed the same error in my project. Here is what i did:
Narrow down the line of code that crashes WebGL.
Go through the painfull process of pinpointing the line of code that
is the source of the error. I had this “luck” that error occured
when I’ve hit button and tried to load UIScene in Addition mode. So
at first I found the script in the scene which when disabled got rid
of the crash. Then I’ve repeated the steps and digged deeper into
lines. After 2 hours of builds, comments etc I’ve found that the code
was not working was setting the new color for UnityEngine.UI.Image.
Which was pretty weird because in playmode everything worked ok.
Find solution.
I think my solution might not be universal but I think there is something going in the Unity gameloop/lifecycle when running WebGL. The source of the problem was that i set the field that was storing my UI.Image in the Start method and then in some function I’ve tried to change the color.
Property was not exposed in inspector.
public class ModuleUI : MonoBehaviour
{
Image qualityBG;
void Start()
{
qualityBG = GetComponent<Image>();
}
then this line below was the cause of the crash
public void Set(Module module)
{
...
qualityBG.color = module.Quality.ToColor();
}
Fixing - so I’ve added [SerializeField] attribute to the field and set it up in editor, dragged the Image into the inspector slot. An it fixed it. I suspect that WebGL build might perform some methods in different order, or maybe the multipleScene loaded together might to do with it. I’m not sure but the field was not set properly with previous code fot WebGL build.
Bonus:
I’ve also fixed some issues that was not critical (not crushing WebGL game) but not working properly as expected in playmode. This also has got to do with trying to set some object in Start() method and then doing something on them. The collection was not set up (probably null) in WebGL.
EffectUI[] effects;
Start()
{
effects = GetComponentsInChildren<EffectUI>();
}
void HideAllEffects()
{
if (effects != null)
for (int i = 0; i < effects.Length; ++i)
effects*.gameObject.SetActive(false);*
} And this also started working when I’ve added [SerializeField] to effects and hook it up in the scene… Hope this will help some of you guys & gals!