Search Unity

Script to spawn items in random locations not working

Discussion in 'Scripting' started by vipes, May 28, 2015.

  1. vipes

    vipes

    Joined:
    May 25, 2015
    Posts:
    8
    Might be because it was wrote for Unity4, but the script doesn't want to work.
    Code (JavaScript):
    1. //Can be renamed to something else, but its a good example!
    2.  
    3. var objectType1: GameObject;
    4.  
    5. var objectType2: GameObject;
    6.  
    7.  
    8.  
    9. //Is the number of items to be spawned that is type 1 or type 2
    10.  
    11. var numberOfType1: int = 5;
    12.  
    13. var numberOfType2: int = 5;
    14.  
    15.  
    16.  
    17. //For checking purposes  to give a count of the number of spawn locations
    18.  
    19. private var type1SpawnCounter: int;
    20.  
    21. private var type2SpawnCounter: int;
    22.  
    23.  
    24.  
    25. //Will store all the possible spawn points for this item type.
    26.  
    27. private var type1SpawnPoints:GameObject[];
    28.  
    29. private var type1SpawnPoints:GameObject[];
    30.  
    31.  
    32.  
    33. //Checks to make sure all type 1 items all spawned
    34.  
    35. private var type1Spawned: int;
    36.  
    37. private var type2Spawned: int;
    38.  
    39.  
    40.  
    41. //Private check to make sure there aren't more items than spawn locations.
    42.  
    43. private var spawnType1: boolean;
    44.  
    45. private var spawnType2: boolean;
    46.  
    47.  
    48.  
    49.  
    50.  
    51. function Start()
    52.  
    53. {
    54.  
    55.     //Setup the private variables
    56.  
    57.     spawnType1 = false;
    58.  
    59.     spawnType2 = false;
    60.  
    61.     type1Spawned = 0;
    62.  
    63.     type2Spawned = 0;
    64.  
    65.     type1SpawnPoints = GameObject.FindGameObjectsWithTag ("SpawnPoint_Type1");
    66.  
    67.     type2SpawnPoints = GameObject.FindGameObjectsWithTag ("SpawnPoint_Type2");
    68.  
    69.  
    70.  
    71.     for (var x in type1SpawnPoints)
    72.  
    73.     {
    74.  
    75.        type1SpawnCounter++; //Count all spawn points tagged SpawnPoint_Type1
    76.  
    77.     }
    78.  
    79.  
    80.  
    81.      for (var y in type2SpawnPoints)
    82.  
    83.     {
    84.  
    85.        type2SpawnCounter++; //Count all spawn points tagged SpawnPoint_Type2
    86.  
    87.     }
    88.  
    89.  
    90.  
    91.     //Error checks
    92.  
    93.     if(numberOfType1 <= type1SpawnCounter)
    94.  
    95.     {
    96.  
    97.         spawnType1 = true;
    98.  
    99.     }
    100.  
    101.         else
    102.  
    103.         {
    104.  
    105.              Debug.LogError("There are more Type 1 items than possible spawn locations!");
    106.  
    107.              return;
    108.  
    109.         }
    110.  
    111.  
    112.  
    113.     if(numberOfType2 <= type2SpawnCounter)
    114.  
    115.     {
    116.  
    117.         spawnType2 = true;
    118.  
    119.     }
    120.  
    121.         else
    122.  
    123.         {
    124.  
    125.              Debug.LogError("There are more Type 2 items than possible spawn locations!");
    126.  
    127.              return;
    128.  
    129.         }
    130.  
    131. }
    132.  
    133.  
    134.  
    135. function Update()
    136.  
    137. {
    138.  
    139.         if(type1Spawned < numberOfType1)
    140.  
    141.         {
    142.  
    143.             SpawnNewType1();
    144.  
    145.             Debug.Log("Spawned a type1 item!");
    146.  
    147.         }
    148.  
    149.  
    150.  
    151.         if(type2Spawned < numberOfType2)
    152.  
    153.         {
    154.  
    155.             SpawnNewType2();
    156.  
    157.             Debug.Log("Spawned a type2 item!");
    158.  
    159.         }
    160.  
    161. }
    162.  
    163.  
    164.  
    165. function SpawnNewType1()
    166.  
    167. {
    168.  
    169.     var randomIndex: int = Random.Range(0, type1SpawnCounter);
    170.  
    171.     var spawnPointReference: Item = type1SpawnPoints[randomIndex].GetComponent("Item");
    172.  
    173.  
    174.  
    175.     if(spawnPointReference.hasItem == false)
    176.  
    177.     {
    178.  
    179.         Instantiate(objectType1, spawnPointReference.transform.position, spawnPointReference.transform.rotation);
    180.  
    181.         type1Spawned ++;
    182.  
    183.         spawnPointReference.hasItem = true;
    184.  
    185.      
    186.  
    187.     }
    188.  
    189. }
    190.  
    191.  
    192.  
    193. function SpawnNewType2()
    194.  
    195. {
    196.  
    197.     var randomIndex: int = Random.Range(0, type2SpawnCounter);
    198.  
    199.     var spawnPointReference: Item = type2SpawnPoints[randomIndex].GetComponent("Item");
    200.  
    201.  
    202.  
    203.     if(spawnPointReference.hasItem == false)
    204.  
    205.     {
    206.  
    207.         Instantiate(objectType2, spawnPointReference.transform.position, spawnPointReference.transform.rotation);
    208.  
    209.         type2Spawned ++;
    210.  
    211.         spawnPointReference.hasItem = true;
    212.  
    213.     }
    214.  
    215. }
    Error code is "The name 'Item' does not denote a valid type ('not found')."
    So, I went looking to see what "Item" is for Unity5. Either I'm bad at searching or it's just not there.