1

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?

Mdl 4848
  • 11
  • 1

1 Answers1

0

Michael Roy found the answer. Because (2 / 8) * radius is equal to 0 (because of how integer division works), vertheight was never being increased, thus causing the infinite loop I originally suspected. Changing (2/8) to 0.25f fixed the issue

Mdl 4848
  • 11
  • 1
  • There is no need to convert (2/8) to 0.25f, You can instead just use (2f/8), (2/8f), or (2f/8f) to cast one or more of the integers to a float value. This will help if you want to use variables instead of constants. I.E. float a = 2f; float b = 8f; (a/b) – Display name Jun 27 '22 at 04:07
  • @Displayname since they are hardcoded "magic values" using `0.25f` would be preferable over introducing an unnecessary float division operation for every iteration ... compiler might even recognize and auto convert this into a const but why take chances ;) – derHugo Jun 27 '22 at 07:51
  • This is true but there is never a reason to do hard coded mathematics in programming such as two divided by eight which leads one to assume that the whole code was not shared with us. Either way, it helps to point out what may be obvious to some and unknown to others. Everyone starts from the beginning after all. – Display name Jun 27 '22 at 10:42