Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Idle Big Numbers Crash

Discussion in 'Scripting' started by Velensel, Mar 9, 2018.

  1. Velensel

    Velensel

    Joined:
    Jan 5, 2016
    Posts:
    1
    Hello!

    I am working on an Idle game in which there are 8 generators.
    Generator1 generates currency
    Generator2 generates Generator1
    etc.

    For debugging I've set Generator2 to 500.
    When Generator1's count gets to ~10.400, Unity crashes.

    I have no idea why. Sometimes when it crashes it gives an error "Array index out of range" in the names array{"Million", "Billion", etc.}.

    This is the format function I use.
    Code (CSharp):
    1. public string FormatDouble(double number)
    2.     {
    3.         double _number = number;
    4.         int i = 0;
    5.         while(_number>=1000)
    6.         {
    7.             _number = _number / 1000;
    8.             i++;
    9.         }
    10.         if(i<2)
    11.         {
    12.             return string.Format ("{0:#,0.###}", number);
    13.         }
    14.         else if(i>=2)
    15.         {
    16.             return string.Format("{0:0.000}", _number) + " " + names [i-2];
    17.         }
    18.         else return "NaN";
    19.     }
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    356
    Clamp your index;
    Code (CSharp):
    1. names[Mathf.Clamp(i-2, 0, names.Length-1)]
     
    Velensel likes this.