• 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
Question by $$anonymous$$ · Aug 26, 2013 at 10:04 AM · c#loopwhilefor

Using for as while.

I need to rerandom a number each time if condition equals false. With while it would look:

 float Distance;
 float minDistance;
 Vector3 Position;

 while(Distance < minDistance)
 {
     Position = new Vector3(Random.Range(50,-50),Random.Range(50,-50),0);
 }

But using while in Update will freeze unity. So how can i do the same using "for" loop ?Thanks!

 float Distance;
 float minDistance;
 Vector3 Position;
 
 for(Distance = Vector3.Distance(transform.position,bla.position);Distance < minDistance;)
 {
     Position = new Vector3(Random.Range(50,-50),Random.Range(50,-50),0);
 }
Comment

People who like this

0 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 sooncat · Aug 26, 2013 at 10:11 AM 0
Share

the var Distance & minDistance do not changed in "while(){}" ?

avatar image $$anonymous$$ · Aug 26, 2013 at 10:15 AM 0
Share

I need to change Position variable inside

avatar image Scribe · Aug 26, 2013 at 06:37 PM 0
Share

Why can you not just use an 'if' statement in an Update function to do this?

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by clunk47 · Aug 26, 2013 at 05:10 PM

Unless you absolutely want to use for or while, you could just do:

     bool condition = false;
     
     void Update()
     {
         if(!condition)
         {
             //Random Range Code Here
             condition = true;
         }
     
     }
     
 
Comment
$$anonymous$$
Dismortus

People who like this

2 Show 0 · 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

Answer by roojerry · Aug 26, 2013 at 10:19 AM

The while loop works as long as you recalculate the Distance from within the loop, like so

 while(Distance < minDistance)
 {
     Position = new Vector3(Random.Range(50,-50),Random.Range(50,-50),0);
     Distance = Vector3.Distance(transform.position,Position);
 }
Comment

People who like this

0 Show 8 · 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 $$anonymous$$ · Aug 26, 2013 at 10:21 AM 0
Share

Oh realy? I thoutht its always freeze when used in Update function!

avatar image $$anonymous$$ · Aug 26, 2013 at 10:22 AM 0
Share

Thank you very much.

avatar image fafase · Aug 26, 2013 at 10:25 AM 0
Share

It does not freeze, it stops it, I would not recommend to use this. The reason is simple, you are not guarantee to get a value before Christmas. It can happen right on or after 50ms or after 10s.

It is wrong to use code that you do not control the processing duration.

avatar image roojerry · Aug 26, 2013 at 10:34 AM 0
Share

True. I just showed how to escape the loop. Due to the randomness of your Position calculation, this would cause random stops in your execution, while waiting for Distance to be greater than minDistance

avatar image $$anonymous$$ · Aug 26, 2013 at 10:34 AM 0
Share

So how can i do this with for? Or anyway to do this?

Show more comments
avatar image

Answer by fafase · Aug 26, 2013 at 10:43 AM

Seeing what you are trying to achieve, let's try another way.

You want to create an object that would be outside the radius of a circle with the player position at the center.

The other way could be to first get a random direction and then get a random distance greater than distance.

 void CreateAtDistance(){
     Vector3 direction = new Vector3(Random.Range(-1.0f,1.0f),Random.Range(-1.0f,1.0f),0);
     direction.Normalize();
     float distance = Random.Range(minDistance, 50);
     direction *= distance;
     NewPosition = Position + direction;
 }

You will have to try this as I cannot...but the idea is as I said, first define a direction. Then you normalize the vector so that it has length 1. Then you multiply by a value between minDistance and max distance (I chose 50). Then you multiply the vector to extend it. Then you do an addition of the position and the direction.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Can't instantiate in loop - crashes unity 1 Answer

For loop not working 1 Answer

Using Loops 1 Answer

What part of my While Loop is causing the game to freeze? (C#) 2 Answers

C# for loop with 2 ints 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