Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JJB653 · Jun 06, 2014 at 10:59 PM · javascriptgameobjectspawnfunction

Stop object spawning on top of each other?

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.

 function spawnenemytype (enemytype:GameObject, amount:int)
 
 {
 
 var spawnLocation:Vector3;
 
 var temp:int;
 isSpawning = true;
 var pos:Vector2 = Vector2.zero;
 
 for (var k:int = 0; k < 1000; k++)
 {
         var randomX:float = Random.Range(minX, maxX);
         var randomY:float = Random.Range(minY, maxY);
         pos = new Vector2(randomX, randomY);
         
 for ( var i:int = 0; i < amount; i++)
 {
     yield WaitForSeconds(1);
  
     temp = Random.Range(1,5);
 
     if (temp == 1)
     {    
         if (spawnedEnemies.Count == 0)
         {
             spawnLocation = new Vector3(pos.x,playerY + offset.y,0);
         }
         else if (Vector2.Distance(spawnLocation , tempGO[i].transform.position) < threshold)
         {    
              break;
          }
     }
 
     if (temp == 2)
     {
 
          if (spawnedEnemies.Count == 0)
         {
             spawnLocation = new Vector3(pos.x,playerY + offset.y,0);
         }
         else if (Vector2.Distance(spawnLocation , tempGO[i].transform.position) < threshold)
         {    
              break;
          }
          
     }
 
 
     if (temp == 3)
     {    
 
          if (spawnedEnemies.Count == 0)
         {
             spawnLocation = new Vector3(pos.x,playerY + offset.y,0);
         }
         else if (Vector2.Distance(spawnLocation , tempGO[i].transform.position) < threshold)
         {    
              break;
          }
          
     }
 
     if (temp == 4)
     {
 
          
          if (spawnedEnemies.Count == 0)
         {
             spawnLocation = new Vector3(pos.x,playerY + offset.y,0);
         }
         else if (Vector2.Distance(spawnLocation , tempGO[i].transform.position) < threshold)
         {    
              break;
          }
          //spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
          //Debug.Log("Creating enemy number4: " );
          
     }
 
  
 tempGO[i] = Instantiate(enemytype,spawnLocation,Quaternion.identity);
 spawnedEnemies.Add(tempGO[i]);
 
 //Debug.Log("how many enemies "+spawnedEnemies.Count);
  enemycounter++;
  
 }
 }
 
 isSpawning = false;
 
 
 
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JJB653 · Jun 09, 2014 at 02:46 PM 0
Share

can someone help me please?

avatar image NoseKills · Jun 09, 2014 at 04:05 PM 0
Share

Which line does the null ref error say it's co$$anonymous$$g from? That already narrows down the problem from 100 lines to one...

avatar image JayOhhh · Jun 09, 2014 at 09:54 PM 0
Share

Load the position of the last spawned object into a variable. $$anonymous$$ake sure that your new random location does not overlap the variable.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MinsukCha · Jun 10, 2014 at 09:49 AM

You access tempGO[i] before it is instantiated. For example, if i = 1 in your for loop, then tempGO[1] is null but it can be accessed when you check Vector2.Distance. When i =1, Only tempGO[0] is instantiated.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

When GameObject is spawned, rotate x 90. 1 Answer

How do you Place/Spawn a GameObject or Prefab in front of Player 2 Answers

Generate random number and set a GameObject active. 1 Answer

Restart this script 3 Answers

Spawn random amount of gameobjects 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges