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. Dismiss Notice

make code shorter

Discussion in 'Editor & General Support' started by FireJojoBoy, Aug 16, 2020.

  1. FireJojoBoy

    FireJojoBoy

    Joined:
    Oct 30, 2019
    Posts:
    65
    can somone tell me how to make this code shorter? o_O
    Code (CSharp):
    1. private void ShowBullets(int howMany)
    2.     {
    3.         if(howMany == 0)
    4.         {
    5.             currentBullets[0].SetActive(false);
    6.             currentBullets[1].SetActive(false);
    7.             currentBullets[2].SetActive(false);
    8.             currentBullets[3].SetActive(false);
    9.             currentBullets[4].SetActive(false);
    10.             currentBullets[5].SetActive(false);
    11.             currentBullets[6].SetActive(false);
    12.             currentBullets[7].SetActive(false);
    13.             currentBullets[8].SetActive(false);
    14.             currentBullets[9].SetActive(false);
    15.             currentBullets[10].SetActive(false);
    16.             currentBullets[11].SetActive(false);
    17.             currentBullets[12].SetActive(false);
    18.             currentBullets[13].SetActive(false);
    19.             currentBullets[14].SetActive(false);
    20.             currentBullets[15].SetActive(false);
    21.         }
    22.         else if(howMany == 1)
    23.         {
    24.             currentBullets[0].SetActive(true);
    25.             currentBullets[1].SetActive(false);
    26.             currentBullets[2].SetActive(false);
    27.             currentBullets[3].SetActive(false);
    28.             currentBullets[4].SetActive(false);
    29.             currentBullets[5].SetActive(false);
    30.             currentBullets[6].SetActive(false);
    31.             currentBullets[7].SetActive(false);
    32.             currentBullets[8].SetActive(false);
    33.             currentBullets[9].SetActive(false);
    34.             currentBullets[10].SetActive(false);
    35.             currentBullets[11].SetActive(false);
    36.             currentBullets[12].SetActive(false);
    37.             currentBullets[13].SetActive(false);
    38.             currentBullets[14].SetActive(false);
    39.             currentBullets[15].SetActive(false);
    40.         }
    41.         else if (howMany == 2)
    42.         {
    43.             currentBullets[0].SetActive(true);
    44.             currentBullets[1].SetActive(true);
    45.             currentBullets[2].SetActive(false);
    46.             currentBullets[3].SetActive(false);
    47.             currentBullets[4].SetActive(false);
    48.             currentBullets[5].SetActive(false);
    49.             currentBullets[6].SetActive(false);
    50.             currentBullets[7].SetActive(false);
    51.             currentBullets[8].SetActive(false);
    52.             currentBullets[9].SetActive(false);
    53.             currentBullets[10].SetActive(false);
    54.             currentBullets[11].SetActive(false);
    55.             currentBullets[12].SetActive(false);
    56.             currentBullets[13].SetActive(false);
    57.             currentBullets[14].SetActive(false);
    58.             currentBullets[15].SetActive(false);
    59.         }
    60.         else if (howMany == 3)
    61.         {
    62.             currentBullets[0].SetActive(true);
    63.             currentBullets[1].SetActive(true);
    64.             currentBullets[2].SetActive(true);
    65.             currentBullets[3].SetActive(false);
    66.             currentBullets[4].SetActive(false);
    67.             currentBullets[5].SetActive(false);
    68.             currentBullets[6].SetActive(false);
    69.             currentBullets[7].SetActive(false);
    70.             currentBullets[8].SetActive(false);
    71.             currentBullets[9].SetActive(false);
    72.             currentBullets[10].SetActive(false);
    73.             currentBullets[11].SetActive(false);
    74.             currentBullets[12].SetActive(false);
    75.             currentBullets[13].SetActive(false);
    76.             currentBullets[14].SetActive(false);
    77.             currentBullets[15].SetActive(false);
    78.         }
    79.         else if (howMany == 4)
    80.         {
    81.             currentBullets[0].SetActive(true);
    82.             currentBullets[1].SetActive(true);
    83.             currentBullets[2].SetActive(true);
    84.             currentBullets[3].SetActive(true);
    85.             currentBullets[4].SetActive(false);
    86.             currentBullets[5].SetActive(false);
    87.             currentBullets[6].SetActive(false);
    88.             currentBullets[7].SetActive(false);
    89.             currentBullets[8].SetActive(false);
    90.             currentBullets[9].SetActive(false);
    91.             currentBullets[10].SetActive(false);
    92.             currentBullets[11].SetActive(false);
    93.             currentBullets[12].SetActive(false);
    94.             currentBullets[13].SetActive(false);
    95.             currentBullets[14].SetActive(false);
    96.             currentBullets[15].SetActive(false);
    97.         }
    98.         else if (howMany == 5)
    99.         {
    100.             currentBullets[0].SetActive(true);
    101.             currentBullets[1].SetActive(true);
    102.             currentBullets[2].SetActive(true);
    103.             currentBullets[3].SetActive(true);
    104.             currentBullets[4].SetActive(true);
    105.             currentBullets[5].SetActive(false);
    106.             currentBullets[6].SetActive(false);
    107.             currentBullets[7].SetActive(false);
    108.             currentBullets[8].SetActive(false);
    109.             currentBullets[9].SetActive(false);
    110.             currentBullets[10].SetActive(false);
    111.             currentBullets[11].SetActive(false);
    112.             currentBullets[12].SetActive(false);
    113.             currentBullets[13].SetActive(false);
    114.             currentBullets[14].SetActive(false);
    115.             currentBullets[15].SetActive(false);
    116.         }
    117.         else if (howMany == 6)
    118.         {
    119.             currentBullets[0].SetActive(true);
    120.             currentBullets[1].SetActive(true);
    121.             currentBullets[2].SetActive(true);
    122.             currentBullets[3].SetActive(true);
    123.             currentBullets[4].SetActive(true);
    124.             currentBullets[5].SetActive(true);
    125.             currentBullets[6].SetActive(false);
    126.             currentBullets[7].SetActive(false);
    127.             currentBullets[8].SetActive(false);
    128.             currentBullets[9].SetActive(false);
    129.             currentBullets[10].SetActive(false);
    130.             currentBullets[11].SetActive(false);
    131.             currentBullets[12].SetActive(false);
    132.             currentBullets[13].SetActive(false);
    133.             currentBullets[14].SetActive(false);
    134.             currentBullets[15].SetActive(false);
    135.         }
    136.         else if (howMany == 7)
    137.         {
    138.             currentBullets[0].SetActive(true);
    139.             currentBullets[1].SetActive(true);
    140.             currentBullets[2].SetActive(true);
    141.             currentBullets[3].SetActive(true);
    142.             currentBullets[4].SetActive(true);
    143.             currentBullets[5].SetActive(true);
    144.             currentBullets[6].SetActive(true);
    145.             currentBullets[7].SetActive(false);
    146.             currentBullets[8].SetActive(false);
    147.             currentBullets[9].SetActive(false);
    148.             currentBullets[10].SetActive(false);
    149.             currentBullets[11].SetActive(false);
    150.             currentBullets[12].SetActive(false);
    151.             currentBullets[13].SetActive(false);
    152.             currentBullets[14].SetActive(false);
    153.             currentBullets[15].SetActive(false);
    154.         }
    155.         else if (howMany == 8)
    156.         {
    157.             currentBullets[0].SetActive(true);
    158.             currentBullets[1].SetActive(true);
    159.             currentBullets[2].SetActive(true);
    160.             currentBullets[3].SetActive(true);
    161.             currentBullets[4].SetActive(true);
    162.             currentBullets[5].SetActive(true);
    163.             currentBullets[6].SetActive(true);
    164.             currentBullets[7].SetActive(true);
    165.             currentBullets[8].SetActive(false);
    166.             currentBullets[9].SetActive(false);
    167.             currentBullets[10].SetActive(false);
    168.             currentBullets[11].SetActive(false);
    169.             currentBullets[12].SetActive(false);
    170.             currentBullets[13].SetActive(false);
    171.             currentBullets[14].SetActive(false);
    172.             currentBullets[15].SetActive(false);
    173.         }
    174.         else if (howMany == 9)
    175.         {
    176.             currentBullets[0].SetActive(true);
    177.             currentBullets[1].SetActive(true);
    178.             currentBullets[2].SetActive(true);
    179.             currentBullets[3].SetActive(true);
    180.             currentBullets[4].SetActive(true);
    181.             currentBullets[5].SetActive(true);
    182.             currentBullets[6].SetActive(true);
    183.             currentBullets[7].SetActive(true);
    184.             currentBullets[8].SetActive(true);
    185.             currentBullets[9].SetActive(false);
    186.             currentBullets[10].SetActive(false);
    187.             currentBullets[11].SetActive(false);
    188.             currentBullets[12].SetActive(false);
    189.             currentBullets[13].SetActive(false);
    190.             currentBullets[14].SetActive(false);
    191.             currentBullets[15].SetActive(false);
    192.         }
    193.         else if (howMany == 10)
    194.         {
    195.             currentBullets[0].SetActive(true);
    196.             currentBullets[1].SetActive(true);
    197.             currentBullets[2].SetActive(true);
    198.             currentBullets[3].SetActive(true);
    199.             currentBullets[4].SetActive(true);
    200.             currentBullets[5].SetActive(true);
    201.             currentBullets[6].SetActive(true);
    202.             currentBullets[7].SetActive(true);
    203.             currentBullets[8].SetActive(true);
    204.             currentBullets[9].SetActive(true);
    205.             currentBullets[10].SetActive(false);
    206.             currentBullets[11].SetActive(false);
    207.             currentBullets[12].SetActive(false);
    208.             currentBullets[13].SetActive(false);
    209.             currentBullets[14].SetActive(false);
    210.             currentBullets[15].SetActive(false);
    211.         }
    212.         else if (howMany == 11)
    213.         {
    214.             currentBullets[0].SetActive(true);
    215.             currentBullets[1].SetActive(true);
    216.             currentBullets[2].SetActive(true);
    217.             currentBullets[3].SetActive(true);
    218.             currentBullets[4].SetActive(true);
    219.             currentBullets[5].SetActive(true);
    220.             currentBullets[6].SetActive(true);
    221.             currentBullets[7].SetActive(true);
    222.             currentBullets[8].SetActive(true);
    223.             currentBullets[9].SetActive(true);
    224.             currentBullets[10].SetActive(true);
    225.             currentBullets[11].SetActive(false);
    226.             currentBullets[12].SetActive(false);
    227.             currentBullets[13].SetActive(false);
    228.             currentBullets[14].SetActive(false);
    229.             currentBullets[15].SetActive(false);
    230.         }
    231.         else if (howMany == 12)
    232.         {
    233.             currentBullets[0].SetActive(true);
    234.             currentBullets[1].SetActive(true);
    235.             currentBullets[2].SetActive(true);
    236.             currentBullets[3].SetActive(true);
    237.             currentBullets[4].SetActive(true);
    238.             currentBullets[5].SetActive(true);
    239.             currentBullets[6].SetActive(true);
    240.             currentBullets[7].SetActive(true);
    241.             currentBullets[8].SetActive(true);
    242.             currentBullets[9].SetActive(true);
    243.             currentBullets[10].SetActive(true);
    244.             currentBullets[11].SetActive(true);
    245.             currentBullets[12].SetActive(false);
    246.             currentBullets[13].SetActive(false);
    247.             currentBullets[14].SetActive(false);
    248.             currentBullets[15].SetActive(false);
    249.         }
    250.         else if (howMany == 13)
    251.         {
    252.             currentBullets[0].SetActive(true);
    253.             currentBullets[1].SetActive(true);
    254.             currentBullets[2].SetActive(true);
    255.             currentBullets[3].SetActive(true);
    256.             currentBullets[4].SetActive(true);
    257.             currentBullets[5].SetActive(true);
    258.             currentBullets[6].SetActive(true);
    259.             currentBullets[7].SetActive(true);
    260.             currentBullets[8].SetActive(true);
    261.             currentBullets[9].SetActive(true);
    262.             currentBullets[10].SetActive(true);
    263.             currentBullets[11].SetActive(true);
    264.             currentBullets[12].SetActive(true);
    265.             currentBullets[13].SetActive(false);
    266.             currentBullets[14].SetActive(false);
    267.             currentBullets[15].SetActive(false);
    268.         }
    269.         else if (howMany == 14)
    270.         {
    271.             currentBullets[0].SetActive(true);
    272.             currentBullets[1].SetActive(true);
    273.             currentBullets[2].SetActive(true);
    274.             currentBullets[3].SetActive(true);
    275.             currentBullets[4].SetActive(true);
    276.             currentBullets[5].SetActive(true);
    277.             currentBullets[6].SetActive(true);
    278.             currentBullets[7].SetActive(true);
    279.             currentBullets[8].SetActive(true);
    280.             currentBullets[9].SetActive(true);
    281.             currentBullets[10].SetActive(true);
    282.             currentBullets[11].SetActive(true);
    283.             currentBullets[12].SetActive(true);
    284.             currentBullets[13].SetActive(true);
    285.             currentBullets[14].SetActive(false);
    286.             currentBullets[15].SetActive(false);
    287.         }
    288.         else if (howMany == 15)
    289.         {
    290.             currentBullets[0].SetActive(true);
    291.             currentBullets[1].SetActive(true);
    292.             currentBullets[2].SetActive(true);
    293.             currentBullets[3].SetActive(true);
    294.             currentBullets[4].SetActive(true);
    295.             currentBullets[5].SetActive(true);
    296.             currentBullets[6].SetActive(true);
    297.             currentBullets[7].SetActive(true);
    298.             currentBullets[8].SetActive(true);
    299.             currentBullets[9].SetActive(true);
    300.             currentBullets[10].SetActive(true);
    301.             currentBullets[11].SetActive(true);
    302.             currentBullets[12].SetActive(true);
    303.             currentBullets[13].SetActive(true);
    304.             currentBullets[14].SetActive(true);
    305.             currentBullets[15].SetActive(false);
    306.         }
    307.         else if (howMany == 16)
    308.         {
    309.             currentBullets[0].SetActive(true);
    310.             currentBullets[1].SetActive(true);
    311.             currentBullets[2].SetActive(true);
    312.             currentBullets[3].SetActive(true);
    313.             currentBullets[4].SetActive(true);
    314.             currentBullets[5].SetActive(true);
    315.             currentBullets[6].SetActive(true);
    316.             currentBullets[7].SetActive(true);
    317.             currentBullets[8].SetActive(true);
    318.             currentBullets[9].SetActive(true);
    319.             currentBullets[10].SetActive(true);
    320.             currentBullets[11].SetActive(true);
    321.             currentBullets[12].SetActive(true);
    322.             currentBullets[13].SetActive(true);
    323.             currentBullets[14].SetActive(true);
    324.             currentBullets[15].SetActive(true);
    325.         }
    326.     }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    check for loops, one option could be something like:
    Code (CSharp):
    1. private void ShowBullets(int howMany)
    2. {
    3.     for (int i=0;i<currentBullets.length;i++)
    4.     {
    5.        currentBullets[i].SetActive(i<howMany); // if i is smaller than howMany, then setactive sets to true
    6.     }
    7. }
     
    FireJojoBoy and Vryken like this.
  3. FireJojoBoy

    FireJojoBoy

    Joined:
    Oct 30, 2019
    Posts:
    65
    thank you :D this is what i needed