I am trying to create a c# script that places cubes along the outside of a sphere. When I try to run this script, Unity stops responding, and task manager shows memory usage increasing. The only way to abort is to kill Unity through task manager. Unity enters Play Mode as expected when the script is disabled. I suspect there is an infinite loop occurring somewhere in the script, but I have been unsuccessful in finding it.
Here is the script causing the issue:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeAnimManager : MonoBehaviour {
public Transform Sphere;
public GameObject CubePrefab;
public float radius;
float radius_at_height = 0;
float horiangle = 0f;
float vertheight;
float xpos;
float ypos;
float zpos;
Vector3 spawnpos;
void Start() {
radius = Sphere.localScale.x / 2;
vertheight = -1 * (Sphere.localScale.x / 2);
for (vertheight = 0; vertheight <= radius; vertheight += (2/8) * radius) {
radius_at_height = Mathf.Sqrt( Mathf.Abs((radius * radius) - (vertheight * vertheight)));
for (horiangle = 0; horiangle <= 2 * Mathf.PI; horiangle += (Mathf.PI / 5)) {
xpos = Mathf.Cos(horiangle) * radius_at_height;
zpos = Mathf.Sin(horiangle) * radius_at_height;
ypos = vertheight;
spawnpos = new Vector3(Sphere.position.x + xpos, Sphere.position.y + ypos, Sphere.position.z + zpos);
Instantiate(CubePrefab, spawnpos, Quaternion.identity);
}
}
}
// Update is called once per frame
void Update() {
}
}
I found another stackoverflow user who had a similar problem, and an infinite loop was the cause, so I believe the same is happening in my script. I just can't see it.
Here is the user in question: Why does Unity get stuck on Application.EnterPlayMode?