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

Stop objects spawning on top of each other?

Discussion in 'Scripting' started by JJB653, Jun 6, 2014.

  1. JJB653

    JJB653

    Joined:
    Apr 18, 2014
    Posts:
    24
    i have this function that spawns enemies in random positions i noticed that they sometimes spawn on top of each other so i tried to use vector2.distance, to compare the old enemy position by the new enemy position but it wont work come up with a error telling me my script is trying to access a object with null this is the code.

    Code (JavaScript):
    1.  
    2.  
    3. function spawnenemytype (enemytype:GameObject, amount:int)
    4.  
    5. {
    6.  
    7. var spawnLocation:Vector3;
    8.  
    9. var temp:int;
    10. isSpawning = true;
    11. var pos:Vector2 = Vector2.zero;
    12.  
    13. for (var k:int = 0; k < 1000; k++)
    14. {
    15.         var randomX:float = Random.Range(minX, maxX);
    16.         var randomY:float = Random.Range(minY, maxY);
    17.         pos = new Vector2(randomX, randomY);
    18.        
    19. for ( var i:int = 0; i < amount; i++)
    20. {
    21.     yield WaitForSeconds(1);
    22.     temp = Random.Range(1,5);
    23.  
    24.     if (temp == 1)
    25.     {  
    26.         if (spawnedEnemies.Count == 0)
    27.         {
    28.             spawnLocation = new Vector3(pos.x,playerY + offset.y,0);
    29.         }
    30.         else if (Vector2.Distance(spawnLocation , tempGO[i].transform.position) < threshold)
    31.         {  
    32.              break;
    33.          }
    34.     }
    35.  
    36.     if (temp == 2)
    37.     {
    38.  
    39.          if (spawnedEnemies.Count == 0)
    40.         {
    41.             spawnLocation = new Vector3(pos.x,playerY + offset.y,0);
    42.         }
    43.         else if (Vector2.Distance(spawnLocation , tempGO[i].transform.position) < threshold)
    44.         {  
    45.              break;
    46.          }
    47.        
    48.     }
    49.  
    50.  
    51.     if (temp == 3)
    52.     {  
    53.  
    54.          if (spawnedEnemies.Count == 0)
    55.         {
    56.             spawnLocation = new Vector3(pos.x,playerY + offset.y,0);
    57.         }
    58.         else if (Vector2.Distance(spawnLocation , tempGO[i].transform.position) < threshold)
    59.         {  
    60.              break;
    61.          }
    62.        
    63.     }
    64.  
    65.     if (temp == 4)
    66.     {
    67.  
    68.        
    69.          if (spawnedEnemies.Count == 0)
    70.         {
    71.             spawnLocation = new Vector3(pos.x,playerY + offset.y,0);
    72.         }
    73.         else if (Vector2.Distance(spawnLocation , tempGO[i].transform.position) < threshold)
    74.         {  
    75.              break;
    76.          }
    77.          //spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
    78.          //Debug.Log("Creating enemy number4: " );
    79.        
    80.     }
    81.  
    82. tempGO[i] = Instantiate(enemytype,spawnLocation,Quaternion.identity);
    83. spawnedEnemies.Add(tempGO[i]);
    84.  
    85. //Debug.Log("how many enemies "+spawnedEnemies.Count);
    86. enemycounter++;
    87. }
    88. }
    89.  
    90. isSpawning = false;
    91.  
    92.  
    93.  
    94. }
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    What line are you getting the NullReferenceException?
     
  3. JJB653

    JJB653

    Joined:
    Apr 18, 2014
    Posts:
    24
    the error is on line 30 this is the error i get

    NullReferenceException: Object reference not set to an instance of an object
    Spawn+$spawnenemytype$58+$.MoveNext () (at Assets/Standard Assets (Mobile)/Scripts/Spawn.js:30)
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you've only pasted in part of your code... so we don't know what line 30 is in the file.
     
  5. JJB653

    JJB653

    Joined:
    Apr 18, 2014
    Posts:
    24
    the real line is 141 but i checked this code and its on line 30. I think i know whats going wrong the enemy isnt getting instantiated still the last line so when its trying to compare tempGo.transform.position it would be null because the enemy hasn't been instantiated yet hmmmm any ideas of another way to get around that?
     
  6. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    As far as I can tell, you check where tempGO is, then you set it on line 82? What are you frying to do here?
     
  7. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Code (csharp):
    1. else if (Vector2.Distance(spawnLocation , tempGO[i].transform.position) < threshold)
    If this is the line you are getting a null reference on, it looks like it is because you have null references in the tempGO array, probably because it hasn't been set.

    The simplest thing is to check for null.
    Code (csharp):
    1. else if (tempGO[i] != null && Vector2.Distance(spawnLocation , tempGO[i].transform.position) < threshold)
     
  8. JJB653

    JJB653

    Joined:
    Apr 18, 2014
    Posts:
    24
    thanks for the help guys i decided to just add a new vector variable called oldposition of type array and after its been instantiated store it the the oldpoisiton array and then compare the next spawned object by the oldposition to generate a new position so they will never be the same. but saying that it still overlaps every now and again :( this is the whole code
    Code (JavaScript):
    1. class Spawn extends UnityEngine.MonoBehaviour
    2.  
    3. {
    4. var enemies:GameObject[] = new GameObject [1];
    5. private var tempGO:GameObject[] = new GameObject[6];
    6. var playerpos:Transform;
    7. var playerY:float;
    8. var enemycounter:int;
    9. var offset:Vector3;
    10. var enemyTypeAllowed:int;
    11. var newPosition : Vector2;
    12. var spawn_position:Vector3;
    13. var timer:float;
    14. var PlayerScript:Player;
    15. var isSpawning:boolean =false;
    16. var minX:float = -4.0f;
    17. var maxX:float = 4.0f;
    18. var minY:float = -4.0f;
    19. var maxY:float = 4.0f;
    20. var threshold:float = 0.5f;
    21.  
    22. private var firstWave:int = 6;
    23. private var secondWave:int = 2;
    24. private var thisTransform : Transform;
    25. private var offsetY:float = 50;
    26. private var minset:float = 20;
    27. private var spawnedEnemies = new List.<GameObject>();
    28. private var tempEnemyScript = new List.<Enemy> ();
    29. //private var tempEnemyScript : Enemy[] = new Enemy[4];
    30.  
    31.  
    32. function Start () {
    33.  
    34.  
    35. playerY = playerpos.position.y;
    36. thisTransform = transform;
    37. //spawnenemytype(enemies[1],firstWave);
    38.  
    39.  
    40.  
    41. }
    42.  
    43.    
    44.    
    45.    
    46. function Update ()
    47. {
    48.  
    49.     if (PlayerScript.flying)
    50.     {
    51.         SpawnEnemies();
    52.     }
    53.  
    54. }
    55.  
    56. function SpawnEnemies ()
    57.  
    58. {
    59.  
    60. //Debug.Log(" "+isSpawning);
    61. offset = new Vector3(0,Random.Range(minset,offsetY),0);
    62.  
    63.  
    64. playerY = playerpos.position.y;
    65. timer +=Time.deltaTime;
    66.  
    67. //newPosition = Random.insideUnitCircle * 5;
    68.  
    69. if (enemycounter <= 2 && !isSpawning)
    70.  
    71. {
    72.     spawnenemytype(enemies[1],firstWave);
    73.     yield WaitForSeconds(2);
    74.     spawnenemytype(enemies[2],firstWave);  
    75. }
    76.  
    77.  
    78. }
    79.  
    80.  
    81.  
    82.  
    83.  
    84.  
    85.  
    86.  
    87.  
    88.  
    89.  
    90.  
    91.  
    92.  
    93.  
    94.  
    95.  
    96.  
    97.  
    98.  
    99.  
    100.  
    101.  
    102.  
    103.  
    104.  
    105.  
    106.  
    107.  
    108.  
    109.  
    110. function spawnenemytype (enemytype:GameObject, amount:int)
    111.  
    112. {
    113.  
    114. var spawnLocation:Vector2;
    115. var oldLocation:Vector2;
    116.  
    117. var temp:int;
    118. isSpawning = true;
    119. var pos:Vector2 = Vector2.zero;
    120.  
    121.         var randomX:float = Random.Range(minX, maxX);
    122.         var randomY:float = Random.Range(minY, maxY);
    123.         pos = new Vector2(randomX, randomY);
    124.        
    125. for ( var i:int = 0; i < amount; i++)
    126. {
    127.     yield WaitForSeconds(1);
    128.     temp = Random.Range(1,5);
    129.  
    130.     if (temp == 1)
    131.     {  
    132.         if (spawnedEnemies.Count == 0)
    133.         {
    134.             spawnLocation = new Vector2(pos.x,playerY + offset.y);
    135.         }
    136.         else if (Vector2.Distance(spawnLocation , oldLocation) < threshold)
    137.         {  
    138.             yield WaitForSeconds(1);
    139.             randomX = Random.Range(minX, maxX);
    140.             pos = new Vector2(randomX, randomY);
    141.              spawnLocation = new Vector2(pos.x,playerY + offset.y);
    142.          }
    143.     }
    144.  
    145.     if (temp == 2)
    146.     {
    147.  
    148.          if (spawnedEnemies.Count == 0)
    149.         {
    150.             spawnLocation = new Vector3(pos.x,playerY + offset.y);
    151.         }
    152.         else if (Vector2.Distance(spawnLocation , oldLocation) < threshold)
    153.         {  
    154.             yield WaitForSeconds(1);
    155.             randomX = Random.Range(minX, maxX);
    156.             pos = new Vector2(randomX, randomY);
    157.             spawnLocation = new Vector2(pos.x,playerY + offset.y);
    158.          }
    159.        
    160.     }
    161.  
    162.  
    163.     if (temp == 3)
    164.     {  
    165.  
    166.          if (spawnedEnemies.Count == 0)
    167.         {
    168.             spawnLocation = new Vector2(pos.x,playerY + offset.y);
    169.         }
    170.         else if (Vector2.Distance(spawnLocation , oldLocation) < threshold)
    171.         {  
    172.             yield WaitForSeconds(1);
    173.             randomX = Random.Range(minX, maxX);
    174.             pos = new Vector2(randomX, randomY);
    175.             spawnLocation = new Vector2(pos.x,playerY + offset.y);
    176.          }
    177.        
    178.     }
    179.  
    180.     if (temp == 4)
    181.     {
    182.  
    183.        
    184.          if (spawnedEnemies.Count == 0)
    185.         {
    186.             spawnLocation = new Vector2(pos.x,playerY + offset.y);
    187.         }
    188.         else if (Vector2.Distance(spawnLocation , oldLocation) < threshold)
    189.         {  
    190.            
    191.             yield WaitForSeconds(1);
    192.             randomX = Random.Range(minX, maxX);
    193.             pos = new Vector2(randomX, randomY);
    194.              spawnLocation = new Vector2(pos.x,playerY + offset.y);
    195.          }
    196.          //spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
    197.          //Debug.Log("Creating enemy number4: " );
    198.        
    199.     }
    200.  
    201. tempGO[i] = Instantiate(enemytype,spawnLocation,Quaternion.identity);
    202. oldLocation = tempGO[i].transform.position;
    203. spawnedEnemies.Add(tempGO[i]);
    204.  
    205. //Debug.Log("how many enemies "+spawnedEnemies.Count);
    206. enemycounter++;
    207. }
    208.  
    209.  
    210. isSpawning = false;
    211.  
    212.  
    213.  
    214. }
    215. }
    216.