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 IMTRIGGERHAPPY9 · Feb 13, 2012 at 04:25 AM · instantiatedistanceloopfor

For loop with Vector3.distance does the loop like 20 times per loop

so i have two vectors that are getting compared with Vector3.distance which is coming out right, but when i try to compare it in a for loop like this

 for( var i = 0; i<= myDistance; i++){
 instantiate(blahblah);
 }

it goes completely crazy it instantiates like 20 or so every frame and it wont stop. i think this has to do with distance internally, but i dont know could some one with more knowledge help me out

Comment
Add comment · Show 1
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 syclamoth · Feb 13, 2012 at 04:43 AM 0
Share

Could you show us where 'myDistance' is calculated? $$anonymous$$eep in $$anonymous$$d that this loop will execute fully once every frame- so if your object is 20 units away, it will always instantiate 20 things per frame.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Feb 13, 2012 at 04:49 AM

If that loop is in Update and your two objects are at a distance 20, obviously you'll got 20 instanciations every frame. You should do that somewhere else, triggered by some event (clic, collisions, etc).

By the way, when you compare vector, you should use the sqrMagnitude instead of distance, so you can spare yourself 2 sqrt.

Comment
Add comment · Show 2 · 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
avatar image IMTRIGGERHAPPY9 · Feb 13, 2012 at 04:58 AM 0
Share

it is in update, but it only happens once per action, ie if i instantiate an object it compares that object with the previous one, it then doesn't go back to that until you instantiate the same object again? does this make sense? does it still matter then?

avatar image Berenger · Feb 13, 2012 at 06:40 AM 0
Share

I'm not sure I get what you mean. Once per action ?

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

6 People are following this question.

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

Related Questions

Can't instantiate in loop - crashes unity 1 Answer

Generate randoms planets with a specific script 0 Answers

Im trying to make an Timer within a for loop, cant figure it out. 1 Answer

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

Creating more objects the closer you get 0 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