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 Tofudude624 · Nov 07, 2014 at 10:14 PM · instantiatearrayloop

Checking all elements of an array?

I am trying to spawn a series of objects where object is at least a certain amount of distance away from any other object. To accomplish this, I store previously generated positions in an array and then check the current position against them. The problem with my code now is that only checks the position of the previously spawned object and not the entire array. How can I check every position in the array? Thanks.

 var MyObject   :GameObject;
 var MyPositions: Vector2[];
 var Temporary  :   Vector2;
 var Current    :       int;
 
 function Start(){
     for (var i : int = 1;i < Current; i++) {
         Temporary.x = Random.Range(-5f,5f);    
         Temporary.y = Random.Range(-5f,5f);    
         if(Vector2.Distance(Temporary,MyPositions[i-1]) <= 1.5f)
             i -= 1;
         if(i == (Current-1))
             MyPositions[Current-1] = Temporary;
     }    
     Instantiate(MyObject,MyPositions[Current-1],transform.rotation);
     Current +=1;
     if(Current<=10)
         Invoke("Start",1);
 }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Uldeim · Nov 07, 2014 at 10:39 PM

That's an interesting approach, but I'd recommend against using recursion for something like this.

A naive implementation of what you're looking for might look like this:

 var TooClose : bool;
 var MAX_SPAWN : int = 10;
 
 function Start() {
    for (var i : int = 0; i < MAX_SPAWN; i++)
    {
          Temporary.x = Random.Range(-5f,5f);    
          Temporary.y = Random.Range(-5f,5f);    
 
          TooClose = false;
          for (var j : int = 0; j < i; j++)
          {
             if(Vector2.Distance(Temporary,MyPositions[j]) <= 1.5f)
             {
               TooClose = true;
               break;
             }
          }
 
          if (TooClose)
          {
            i--;
            continue;
          }
 
          MyPositions[i] = Temporary;
          Instantiate(MyObject,MyPositions[i],transform.rotation);
    }
  }


As a side note, having done this in the past, I recommend against generating distance separated objects like this. It can lead to huge load times if you get an unlucky couple of random values, and infinite load times if you accidentally mess up your distance to size-of-map ratio. I'd look into two dimensional random distribution algorithms instead, if I were you. Unfortunately, I don't know any off the top of my head to recommend.

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

27 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 avatar image avatar image

Related Questions

Puzzle + Grid Instantiate - Random 2 Answers

How to compare and detect overlapping gameObject Prefabs using maths 0 Answers

How to loop through and remove Instantiate Gameobject clones 2 Answers

OverlapSphere for parallel arrays 1 Answer

IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 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