Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Max amount of variables? Crashing Chrome and IE, running in Firefox...

Discussion in 'Editor & General Support' started by dice929, Apr 5, 2013.

  1. dice929

    dice929

    Joined:
    Apr 5, 2013
    Posts:
    3
    Hi there,

    my unity web project runs great with Firefox. But there is an "internal error" when using Chrome or IE.
    I found the exact part in my code where it starts to crash on Chrome/IE but can only guess where the problem is.
    When excluding the part from this point it runs on Chrome/IE as well.

    Short description:
    While initialising lots of variables to define monster waves for my tower defense game at a certain point Chrome/IE crashes,
    while Firefox still runs the web application. I use multi-dimensional arrays which size is predefined by static integers. The
    code to define my monster waves weights about ~3000 lines of code (10 levels à 20 waves à 50 different monsters...). It seems
    that at a certain point the variable definitions are "too much" for Chrome/IE...

    My assumption: Chrome/IE use a maximum of variables, breaking the limit causes the crash.

    Anyone knowing this problem?

    Strange: When I initialize all the monsters with one big loop, it also runs well in Chrome/IE, see last code part:


    Variable definitions:

    Code (csharp):
    1. private static var max_challenges:      int = 11;
    2. private static var max_monster :        int = 61; //maximale anzahl an monster pro welle
    3. private static var max_waves :          int = 31; //maximale anzahl an wellen pro challenge
    4. private static var max_addition :       int = 30; //maximale anzahl an zusätzlichen Kategorien
    5. private static var max_monstertype :    int = 21; //maximale anzahl an verschiedenen monster typen
    6. private static var max_monsterlevel :   int = 11; //maximales monster level
    7.  
    8. private var wave_count :        int[] = new int[max_challenges];
    9. private var monster_count :     int[,] = new int[max_challenges,max_waves];
    10. private var monster_type :      int[,,] = new int[max_challenges,max_waves,max_monster];
    11. private var monster_level :     int[,,] = new int[max_challenges,max_waves,max_monster];
    12. private var monster_strength :  int[,,] = new int[max_challenges,max_waves,max_monster];
    13. private var monster_armor :     int[,,] = new int[max_challenges,max_waves,max_monster];
    14. private var monster_addition :  int[,,,] = new int[max_challenges,max_waves,max_monster,max_addition];

    Extract of the code producing the crash on Chrome/IE:

    Code (csharp):
    1. var challenge : int = 10;
    2. var wave : int;
    3. var monster : int;
    4. var i : int;
    5.  
    6. //-----------------------------------------------------------------
    7. //Defining monsters for challenge 1
    8. challenge = 1;
    9. wave_count[challenge] = 8;
    10. //-->
    11.     //Defining monsters for wave 1
    12.     wave = 1;
    13.     monster_count[challenge,wave] = 1;
    14. //----->
    15.     monster_type[challenge,wave,monster] = 1;
    16.     monster_level[challenge,wave,monster] = 1;
    17.     monster_strength[challenge,wave,monster] = 1;
    18.     monster_armor[challenge,wave,monster] = 1;
    19.     for(i = 0; i < max_addition; i++) {
    20.         monster_addition[challenge,wave,monster,i] = 0;
    21.     }
    22.  
    23. ...
    24.  
    25. //-->
    26.     //Defining monsters for wave 8
    27.     wave = 8;
    28.     monster_count[challenge,wave] = 15;
    29. //----->
    30. for(monster = 1; monster <= 5; monster++) { //alle monster gleich abfrühstücken
    31.     monster_type[challenge,wave,monster] = 2;
    32.     monster_level[challenge,wave,monster] = 1;
    33.     monster_strength[challenge,wave,monster] = 1;
    34.     monster_armor[challenge,wave,monster] = 1;
    35.     for(i = 0; i < max_addition; i++) {
    36.         monster_addition[challenge,wave,monster,i] = 0;
    37.     }
    38. }
    39. for(monster = 6; monster <= 10; monster++) {    //alle monster gleich abfrühstücken
    40.     monster_type[challenge,wave,monster] = 2;
    41.     monster_level[challenge,wave,monster] = 2;
    42.     monster_strength[challenge,wave,monster] = 1;
    43.     monster_armor[challenge,wave,monster] = 1;
    44.     for(i = 0; i < max_addition; i++) {
    45.         monster_addition[challenge,wave,monster,i] = 0;
    46.     }
    47. }
    48. for(monster = 11; monster <= 15; monster++) {   //alle monster gleich abfrühstücken
    49.     monster_type[challenge,wave,monster] = 2;
    50.     monster_level[challenge,wave,monster] = 3;
    51.     monster_strength[challenge,wave,monster] = 1;
    52.     monster_armor[challenge,wave,monster] = 1;
    53.     for(i = 0; i < max_addition; i++) {
    54.         monster_addition[challenge,wave,monster,i] = 0;
    55.     }
    56. }
    57. //-----------------------------------------------------------------
    58. //Defining monsters for challenge 1
    59. challenge = 2;
    60. wave_count[challenge] = 14;
    61. //-->
    62. ...

    Code (csharp):
    1. //all-including-loop runs great with Firefox/Chrome/IE
    2. for(challenge = 1; challenge < max_challenges; challenge++) {
    3.     for(wave = 1; wave < max_waves; wave++) {
    4.         for(monster = 1; monster <= max_monster; monster++) {   //alle monster gleich abfrühstücken
    5.             monster_type[challenge,wave,monster] = 1;
    6.             monster_level[challenge,wave,monster] = 1;
    7.             monster_strength[challenge,wave,monster] = 1;
    8.             monster_armor[challenge,wave,monster] = 1;
    9.             for(i = 0; i < max_addition; i++) {
    10.                 monster_addition[challenge,wave,monster,i] = 0;
    11.             }
    12.         }
    13.     }
    14. }
    ------------------------------------
    My Project: a roleplay tower defense browser game developed with unity
    visit my homepage
     
  2. chjacobsen

    chjacobsen

    Joined:
    Oct 8, 2012
    Posts:
    58
    I very much doubt there is such a thing as too many variables. However, you should know that static arrays are fast because they allocate memory in advance. If you create an array of 3000 integers, it is going to allocate memory for these 3000 integers even though they haven't been touched yet. 3000 integers equals 12000 bytes, which isn't massive, but it is possible to declare very large static arrays which cause memory issues. If your program crashes due to static array declarations, memory is a much more likely issue than the actual number of declarations.
     
  3. dice929

    dice929

    Joined:
    Apr 5, 2013
    Posts:
    3
    Thanks chjacobsen for your reply. I know about allocating static arrays and memory issues behind it. I think a cache or buffer overflow was produced using Chrome/IE because all the code was successively written. I splitted my "spaghetti code" into several functions that now run simultaneous and don't crash the plugin any more.

    Crash is still unknown but now fixed. Here is a short description from my website, maybe interesting for people with equal problems:

    Starting from version 30.01.2013 (with the huge update) the browsergame could not be loaed during a fatal content error produced with Chrome and IE after loading the webfile that is about 8 MByte big. Our first thought: Something has to crash while initialisation runs or to be precised calling the Start()-functions throws the error. Bit by bit we reverted all major changes from the last version and extracted the problem faster than we thought: the new implemented monster-wave-generator was throwing the error. The ca. 3000 Lines of spaghetti-code could not be translated by Chrome/IE and caused the unity web player plugin to crash. Strangely enough this version of the game was running well with Unity-Editor, Firefox and Safari. We noticed this error very late because we were testing everything exclusively with Firefox. The actual reason is still unknown. Maybe the plugin crashed because of a browser cache overflow. A post at the official Unity-forum could not name the error as well. After implementing the monster-wave-generator in a more generic way and excluding code in some external functions, the new version of Reach The Roof now runs well with Chrome and IE.

    Lessons Learned:

    outsource often used code in external functions
    keep on weekly documentation and changelog-news
    spread tests on different browsers