Strange JS array problem.

Hey everyone,

I'm relatively new to Javascript and was experimenting with JS arrays when I kept running into a problem with the arrays clearing themselves.

Here's what I'm doing; I declare an array in one script, manipulate its contents and then add that script to multiple game objects. I move the objects in between scenes and I can see that the contents are kept intact but in certain cases it seemed like the arrays were being cleared for some reason.

I put in a bunch of debug.log lines that print the length of the arrays at certain points throughout the flow of gameplay and nothing seemed to be wrong. I then started placing the debug.log lines while the game is running and that's when I started experiencing the problem or was able to recreate it at will.

Is this a common problem? I never ran into anything saying arrays clear themselves like this and I can't find anything when I search for the error. Thanks in advance!

You say that you are moving between scenes, do you have the the objects set so they won’t destroy on load? If you move between a scene then all the changes you made to a gameobject are destoyed unless you tell it to keep itself intact

Check it out here

Also Javascript arrays tend to reset themselves when the script is being reloaded. If your script is being reloaded this may also be a problem.