Stuck on "Waiting for Unity's code to finish executing", unable to enter playmode

I was making a script for sun rotation, and at some point, to check whether it works or not i tried to enter play mode but… it just stuck on “Waiting for Unity’s code to finish executing” and I had to close Unity in task manager. I tried multiple times, still stuck. Only when I disable the script as an object’s component, then I can enter play mode. Here is the script (its not yet done, dont judge, and Im new to code):

DayAndNightCycle.cs (1.4 KB)

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

public class DayAndNightCycle : MonoBehaviour
{
    [Header("Time Settings")]
    [SerializeField] private float timeSpeed = 1f;
    [SerializeField] private float UnitsPerSecond = 1f;
    [SerializeField] private float SetSunAngle = 0f;
    [SerializeField] private bool isSunRotating = true;

    private float sunRotation;
    private float RealSunAngle;
    private float OldSunAngle;


    [Header("Light Settings")]
    [SerializeField] private Transform sunLight;

    private void Start()
    {
        SetSunAngle = 0f;
        sunLight = GameObject.Find("Sun").GetComponent<Transform>();
        OldSunAngle = SetSunAngle;
        RealSunAngle = OldSunAngle;
    }

    private void Update()
    {
        NaturalSunRotation();

       if (SetSunAngle != OldSunAngle)
        {
            ForceRotateByAngle();
        }
    }

    void NaturalSunRotation()
    {
        while (isSunRotating == true)
        {
            sunRotation = UnitsPerSecond * Time.deltaTime;

            Debug.Log(sunRotation);
            sunLight.transform.Rotate(0f, sunRotation, 0f);
        }
    }
    void ForceRotateByAngle()
    {
        RealSunAngle = RealSunAngle - OldSunAngle + SetSunAngle;
        sunLight.transform.Rotate(0f, RealSunAngle, 0f);
        OldSunAngle = RealSunAngle;
    }


}


Any ideas why it stucks?

You have a while loop that goes infinite in NaturalSunRotation.

You don’t want enter a while-loop that never exits in Update (or in general). Unity will stay in that loop forever, looking up your program.

The point of Update is that it already itself is part of the greater player loop, which ticks over once every frame. You only want to be rotating a little bit per-frame, so no loop is needed.

1 Like

Thank you so much, I dont know what other way I would discover this…

1 Like