How can I find what is making unity not respond after a second of playing the project?

The project is simple, just two sprites and two scripts, One script rotates the objects so they face the camera. The other script is more complicated. It is an attempt to make the second sprite orbit around the first. When I play the project it looks like it quickly makes one orbit and then the unity window stops responding. I have made some console commands to check values of variables, and they seem ordinary. I have looked for the log files of unity, but I haven’t found errors in the one log file I found. I looked for the other log file, but there was no file in the folder.
This is my orbit script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Orrery2 : MonoBehaviour
{
    public Transform Center;
    public Transform Other;
    float x;
    float y;
    float z;
    float Begining = 1;
    float Elapsed;

    void FixedUpdate()
    {
        Elapsed = Begining++;
        PositionFromDate();
        Debug.Log(Elapsed);
        Debug.Log("x:" + x + ", " + "y:" + y + ", " + "z:" + z);
        Other.transform.position = new Vector3(x,y,z);
    }
    void PositionFromDate()
    {
        float a = 1f * Elapsed; //Semi-Major Axis
        float e = 0.8f * Elapsed; //Eccentricity
        float i = 1f * Elapsed; //Inclination
        float W = 1f * Elapsed; //Longitude of the Ascending Node
        float w = 1f; //Argument of Periapsis
        float M = 1f; //Mean Anomaly
        float E = 1f; //Eccentric Anomaly

        //Newton's Method
        E = M;
        while (true)
        {
            float dE = (E - e * Mathf.Sin(E) - M) / (1 - e * Mathf.Cos(E));
            E -= dE;
            if (Mathf.Abs(dE) < 1e-3){
                break;
            }
        }

        //Determine P and Q, 2d coordinate system in plane of orbit
        float P = a * (Mathf.Cos(E) - e);
        float Q = a * Mathf.Sin(E) * Mathf.Sqrt(Mathf.Abs(1 - Mathf.Pow(e, 2)));

        Debug.Log("Line 46 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

        // rotate by argument of periapsis
        x = Mathf.Cos(w) * P - Mathf.Sin(w) * Q;
        y = Mathf.Sin(w) * P + Mathf.Cos(w) * Q;

        Debug.Log("Line 52 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
       
        // rotate by inclination
        z = Mathf.Sin(i) * x;
        x = Mathf.Cos(i) * x;

        Debug.Log("Line 58 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

        // rotate by longitude of ascending node
        float xTemp = x;
        x = Mathf.Cos(W) * xTemp - Mathf.Sin(W) * y;
        y = Mathf.Sin(W) * xTemp + Mathf.Cos(W) * y;

        Debug.Log("Line 65 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
    }
}

My money is on the while loop not hitting the break condition.

You can debug this by temporarily converting the contacting function into a coroutine and yielding in the while loop, so you can log the values of the variables.

1 Like

Thank you, The values from the CoRoutine didn’t show any problem, but it highlighted that spot, and so I changed the while (true) to the condition in the if statement, like this

//Newton's Method
        E = M;
        while (Mathf.Abs(dE) < 1e-3)
        {
            dE = (E - e * Mathf.Sin(E) - M) / (1 - e * Mathf.Cos(E));
            E -= dE;
            Debug.Log("E:" + E + ", " + "e:" + e + ", " + "M:" + M + ", " + "dE:" + dE);
        }

No crashes now! Just a completely wonky “orbit” that jumps everywhere. Thank you.

1 Like

Usually when unity freezes you can look at your cpu usage. If it’s using cpu then you are usually stuck in an infinite loop in your code.