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?