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 *= 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*.transform.position = Pos;_
_ }_
_}*_