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 Alex_K98 · Mar 25, 2015 at 01:07 PM · loopfreezeinfinitehangbreak

'brake' statement makes Unity hang ?

Can someone explain why this code makes Unity hang?

This tries to find a non-intersecting position for objects generated. 'prefab' is a common Unity sphere which size is set to (50,50,50). Default 'SphereCollider' is set to 'isTrigger' (just in case).

I suspect 'break' call being a problem, because I have experienced strange behavior of 'return' call.

Important note: CalcPositions() works without 'hang' problem in other engine!!!

Any help would be much appreciated.

Here is the code:

 public GameObject prefab;    //sphere of size (50,50,50)
 GameObject[] go;            //array of these spheres
 int Count = 3;                //size of array
 float ObjectRadius = 50;    //scalar of sphere size
 float SceneRadius = 250;    //area of position value generation
 Vector3 pos = new Vector3(0,0,0);
     
 void Awake () {
     go = new GameObject[Count];    //init of array
     Generate ();                //generating objects
     CalcPositions ();            //calculating positions
 }
     
 void Generate () {
     for (int i = 0; i < Count; i++) {
         Quaternion rot = Quaternion.identity;
         go[i] = Instantiate (prefab, pos, rot) as GameObject;
     }
 }
     
 void CalcPositions () {
     int j = 0;
     for (int i = 0; i < Count; i++) {
         Pos.x = Random.Range (-SceneRadius, SceneRadius);
         Pos.y = 0; Pos.z = 0;
         while (j < i) {
             for (j = 0; j < i; j++) {
                 Vector3 tempPos = go[j].transform.position;
                     if (Vector3.Distance(tempPos, Pos) < ObjectRadius) {
                         break;
                     }
             }
             Pos.x = Random.Range (-SceneRadius, SceneRadius);
         }
         go[i].transform.position = Pos;
     }
 }
 
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

Answer by Jeffom · Mar 25, 2015 at 01:20 PM

the break statement is breaking out of the for before j < i, and that causes the code to freeze.

Comment
Add comment · Show 7 · 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 Owen-Reynolds · Mar 25, 2015 at 02:31 PM 0
Share

In other words, you (the OP) wrote a regular boring infinite loop. There's nothing special about the break statement.

avatar image Alex_K98 · Mar 25, 2015 at 02:45 PM 0
Share

Gentlemen, the thing is that CalcPositions function hangs randomly. It can generate non-intersecting positions nicely, but next time it can run into 'hang' even if there is triple space reserve to find correct position. That is the problem and weirdness!

avatar image Alex_K98 · Mar 25, 2015 at 02:57 PM 0
Share

Jeffom, how would you suggest to overcome 'j < i' that you think causing the freeze? Thanks in advance.

avatar image Jeffom · Mar 25, 2015 at 03:20 PM 0
Share

You could change the while condition, or make a flag for a better check to break out of the while loop.

avatar image Bonfire-Boy · Mar 25, 2015 at 04:45 PM 0
Share

I take the points raised already, and the while thing doesn't look like a great way of doing this. But it's still not clear to me why it's not working. There should be enough space for it to be able to randomly find 3 positions that aren't that close to each other (although shouldn't the $$anonymous$$imum distance be 2*ObjectRadius?).

I would add some logging, in particular to show what the values of i, j, and Pos are when it detects an object too close to Pos.

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

Game random freeze / hang up / stuck 0 Answers

Unity Keeps Freezing on Play 5 Answers

Unity 4.1 hangs/freezes on startup (Mac) 0 Answers

Scripts freezes game for a few seconds after build. 1 Answer

2D Vertical scrolling infinite background 1 Answer


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