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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Better way to Write this script?

Discussion in 'Scripting' started by Jorjor210, May 22, 2015.

  1. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I am currently trying to make an enemy spawn at 1 of the 22 points randomly. And although it works perfectly fine! I'm just curious if there's a way to maybe re-write this simpler?

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var cube : GameObject;
    4. var spawn_position;
    5. var spawn_position2;
    6. var spawn_position3;
    7. var spawn_position4;
    8. var spawn_position5;
    9. var spawn_position6;
    10. var spawn_position7;
    11. var spawn_position8;
    12. var spawn_position9;
    13. var spawn_position10;
    14. var spawn_position11;
    15. var spawn_position12;
    16. var spawn_position13;
    17. var spawn_position14;
    18. var spawn_position15;
    19. var spawn_position16;
    20. var spawn_position17;
    21. var spawn_position18;
    22. var spawn_position19;
    23. var spawn_position20;
    24. var spawn_position21;
    25. var spawn_position22;
    26. var timer = 0.0;
    27. var min =0;
    28. var max = 0;
    29. function Start () {
    30.     var num = Random.Range(1,22);
    31.    
    32.     //1
    33.     if (num ==1) {
    34.     spawn_position = Vector3(-9.78,0,0);
    35.     var temp_spawn_cube = Instantiate(cube, spawn_position, Quaternion.identity);
    36.    
    37.     }
    38.    
    39.         //2
    40.     if (num ==2) {
    41.     spawn_position2 = Vector3(-6.46,0,0);
    42.     var temp_spawn_cube2 = Instantiate(cube, spawn_position2, Quaternion.identity);
    43.    
    44.     }
    45.    
    46.         //3
    47.     if (num ==3) {
    48.     spawn_position3 = Vector3(-3.45,0,0);
    49.     var temp_spawn_cube3 = Instantiate(cube, spawn_position3, Quaternion.identity);
    50.    
    51.     }
    52.    
    53.         //4
    54.     if (num ==4) {
    55.     spawn_position4 = Vector3(2.92,0,0);
    56.     var temp_spawn_cube4 = Instantiate(cube, spawn_position4, Quaternion.identity);
    57.    
    58.     }
    59.     //1
    60.     if (num ==5) {
    61.     spawn_position5 = Vector3(5.90, 0,0);
    62.     var temp_spawn_cube5 = Instantiate(cube, spawn_position5, Quaternion.identity);
    63.    
    64.     }
    65.    
    66.         //2
    67.     if (num ==6) {
    68.     spawn_position6 = Vector3(9.44,0,0);
    69.     var temp_spawn_cube6 = Instantiate(cube, spawn_position6, Quaternion.identity);
    70.    
    71.     }
    72.    
    73.         //7
    74.     if (num ==7) {
    75.     spawn_position7 = Vector3(-9.95,-2.36,0);
    76.     var temp_spawn_cube7 = Instantiate(cube, spawn_position7, Quaternion.identity);
    77.    
    78.     }
    79.    
    80.         //8
    81.     if (num ==8) {
    82.     spawn_position8 = Vector3(-6.46,-2.41,0);
    83.     var temp_spawn_cube8 = Instantiate(cube, spawn_position8, Quaternion.identity);
    84.    
    85.     }
    86.             //9
    87.     if (num ==9) {
    88.     spawn_position9 = Vector3(-5.96,-2.42,0);
    89.     var temp_spawn_cube9 = Instantiate(cube, spawn_position9, Quaternion.identity);
    90.    
    91.     }
    92.    
    93.         //10
    94.     if (num ==10) {
    95.     spawn_position10 = Vector3(9.22,-2.47,0);
    96.     var temp_spawn_cube10 = Instantiate(cube, spawn_position10, Quaternion.identity);
    97.    
    98.     }
    99.     //11
    100.     if (num ==11) {
    101.     spawn_position11 = Vector3(-9.66, 4.66,0);
    102.     var temp_spawn_cube11 = Instantiate(cube, spawn_position11, Quaternion.identity);
    103.    
    104.     }
    105.    
    106.         //12
    107.     if (num ==12) {
    108.     spawn_position12 = Vector3(-6.52,4.83,0);
    109.     var temp_spawn_cube12 = Instantiate(cube, spawn_position12, Quaternion.identity);
    110.    
    111.     }
    112.    
    113.         //13
    114.     if (num ==13) {
    115.     spawn_position13 = Vector3(5.96,-4.72,0);
    116.     var temp_spawn_cube13 = Instantiate(cube, spawn_position13, Quaternion.identity);
    117.    
    118.     }
    119.    
    120.         //14
    121.     if (num ==14) {
    122.     spawn_position14 = Vector3(9.22,-4.82,0);
    123.     var temp_spawn_cube14 = Instantiate(cube, spawn_position14, Quaternion.identity);
    124.    
    125.     }
    126.     //15
    127.     if (num ==15) {
    128.     spawn_position15 = Vector3(-9.95, -5.07,0);
    129.     var temp_spawn_cube15 = Instantiate(cube, spawn_position15, Quaternion.identity);
    130.    
    131.     }
    132.    
    133.         //16
    134.     if (num ==16) {
    135.     spawn_position16 = Vector3(5.9,-5.07,0);
    136.     var temp_spawn_cube16 = Instantiate(cube, spawn_position16, Quaternion.identity);
    137.    
    138.     }
    139.    
    140.         //17
    141.     if (num ==17) {
    142.     spawn_position17 = Vector3(5.9,-4.7,0);
    143.     var temp_spawn_cube17 = Instantiate(cube, spawn_position17, Quaternion.identity);
    144.    
    145.     }
    146.    
    147.         //18
    148.     if (num ==18) {
    149.     spawn_position18 = Vector3(-9.95,-5.07,0);
    150.     var temp_spawn_cube18 = Instantiate(cube, spawn_position18, Quaternion.identity);
    151.    
    152.     }
    153.             //19
    154.     if (num ==19) {
    155.     spawn_position19 = Vector3(9.22,-5.07,0);
    156.     var temp_spawn_cube19 = Instantiate(cube, spawn_position19, Quaternion.identity);
    157.    
    158.     }
    159.    
    160.         //20
    161.     if (num ==20) {
    162.     spawn_position20 = Vector3(-6.63,-5.07,0);
    163.     var temp_spawn_cube20 = Instantiate(cube, spawn_position20, Quaternion.identity);
    164.    
    165.     }
    166.             //21
    167.     if (num ==21) {
    168.     spawn_position21 = Vector3(9.22,-5.07,0);
    169.     var temp_spawn_cube21 = Instantiate(cube, spawn_position21, Quaternion.identity);
    170.    
    171.     }
    172.    
    173.         //22
    174.     if (num ==22) {
    175.     spawn_position22 = Vector3(-0.35,4.25,0);
    176.     var temp_spawn_cube22 = Instantiate(cube, spawn_position22, Quaternion.identity);
    177.    
    178.     }
    179.     }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
  3. JaviOverflow

    JaviOverflow

    Joined:
    May 22, 2015
    Posts:
    12
    Try to use arrays with spawn_positions and temp_spawn_cubes. You can use num var as index of the arrays.

    You could try to use any kind of hash mapping for temp_spawn_cubes in order to save space of non-instantiated cubes. If mapping seems a little hard for you, keeping the cubes as arrays is good enough as long as you keep the number of cubes around 22.
     
  4. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    I'm a C# person and never use JS, so not sure if I got this quite right, but maybe like this:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var cube : GameObject;
    4.  
    5. var spawnPositions =
    6. [
    7.     // Fill in your spawn position numbers in here, I used 0 as filler.
    8.     // Assumes you want very specific hand-picked spawn positions.
    9.     Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0),
    10.     Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0),
    11.     Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0),
    12.     Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0),
    13.     Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(0, 0, 0),
    14.     Vector3(0, 0, 0), Vector3(0, 0, 0)
    15. ];
    16.  
    17. var timer = 0.0f;
    18. var min = 0;
    19. var max = 0;
    20.  
    21. function Start()
    22. {
    23.     var num = Random.Range(0, 22);
    24.     var temp_spawn_cube = Instantiate(cube, spawnPositions[num], Quaternion.identity);
    25. }