I’m creating a series of cubes arranged in a circle and then spreading them out such that no cube is colliding with another. Around half of the time this works perfectly and the other half of the time Unity stops responding.
public IEnumerator SpreadRooms(List<GameObject> rooms) {
for(int i = 0;i<rooms.Count; i++) {
GameObject room = rooms*;*
-
RoomSpacer rs = room.GetComponent<RoomSpacer>();*
-
yield return null;*
-
Debug.Log(rs.count);*
-
Debug.Log(rs.dir);*
-
while(rs.count > 0) {*
-
if(rs.dir == Vector3.zero) {*
-
rs.dir = new Vector3(Random.Range(-3f, 3f), 0, Random.Range(-3f, 3f));*
-
Debug.Log("Adjusting " + rs.dir);*
-
}*
_ room.transform.position += rs.dir * 30 * Time.deltaTime;_
-
yield return null;*
-
}*
-
Debug.Log(i);*
-
Debug.Log(rooms.Count);*
-
}*
}
According to the console it looks like the crash occurs in the while loop which made me think that it might be an infinite loop but I dont see why that would occur. Each cube has a dir for it to move in and none of them are zero including the ones that cause the crash. rs.count tracks the number of objects that the cube is colliding with.
Ive been testing this with 50 objects and it typically crashes in the 40s for some reason but computer resources dont seem to be an issue.