- Home /
unity keeps freezing when i run this script,unity keeps crashing whenever i run my daylight cycle script, why?
my daylight cycle is crashing unity whenever it runs (not so much crashing as permanently freezing) if anyone could point out why it would be awesome this is my first time uploading a question so i have no idea how to format it
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DayCycle : MonoBehaviour { public Light lt; public int daytime = 5; public int nighttime = 5; public bool Up; public bool Down; private float value; public int time; private bool running; // Use this for initialization void Start() { n2d(); lt = GetComponent(); lt.intensity = 1; } // Update is called once per frame void Update() { } private void FixedUpdate() { if (Up == true) { value = value + 0.05f; lt.intensity = value; } if (Down == true) { value = value - 0.05f; lt.intensity = value; } } IEnumerator day() { Debug.Log("its daytime"); time = 1; yield return new WaitForSeconds(daytime); d2n(); } IEnumerator night() { Debug.Log("its nighttime"); time = 3; yield return new WaitForSeconds(nighttime); n2d(); } void n2d() { Debug.Log("its dawn"); time = 0; running = true; while (running) { Up = true; if (value >= 1f) { Up = false; value = 1; lt.intensity = value; running = false; } } StartCoroutine(day()); } void d2n() { Debug.Log("its dusk"); time = 2; running = true; while (running) { Down = true; if (value <= 0f) { Down = false; value = 0; lt.intensity = value; running = false; } } StartCoroutine(night()); } },
In the future, please make an effort to follow the FAQ before posting a question, otherwise your post may be rejected.
Why did my question get closed or rejected? If your question was closed or rejected, you can still view any comments left for that closed post in your activity panel on your Unity Answers profile page. If you are just starting out with Unity we suggest checking out the Learn section, as there exists several tutorials, documentation and live training sessions. We want to encourage users to look up information themselves, as this is the best way to learn. If you can easily find what you are looking for in the Documentation, Learn section or by simply searching in Answers or the Forum, we suggest you doing that before asking your question. There are several reasons for questions to be rejected or closed, each moderator makes the decision to publish or reject a post according to their own judgement and following the $$anonymous$$oderator Guidelines. Some reasons for getting a post rejected: - Badly formatted question: code needs to be formatted using the 101/010 button, break your post up into several parts so it's understandable what you are asking, and make sure there is a question in your post - Asking us to fix your code: more than likely, you simply need to get a better understanding of basic program$$anonymous$$g. Having a look at the Scripting tutorials on the Learn page will help you become more familiar with scripting in Unity
Answer by JaredHD · Dec 03, 2017 at 08:30 AM
Formatting Basics
Hello, ok so for formatting code. If you look at the top of this inputfield, when you enter text. You can see a bunch of options, such as bold, italic, hyperlink, etc. When you want to enter code you press the 101010 button and paste your code, and it will look like so. using System.Collections;
using UnityEngine;
public class DayCycle : MonoBehaviour
{
public Light lt;
public int daytime = 5;
public int nighttime = 5;
public bool up;
public bool down;
private float value;
public int time;
private bool running;
private void Start()
{
NightToDay();
lt = GetComponent<Light>();
lt.intensity = 1;
}
private void Update()
{
if (up == true)
{
value = value + 0.05f;
lt.intensity = value;
}
if (down == true)
{
value = value - 0.05f;
lt.intensity = value;
}
}
private IEnumerator day()
{
Debug.Log("its daytime");
time = 1;
yield return new WaitForSeconds(daytime);
DayToNight();
}
private IEnumerator night()
{
Debug.Log("its nighttime");
time = 3;
yield return new WaitForSeconds(nighttime);
NightToDay();
}
private void NightToDay()
{
Debug.Log("its dawn");
time = 0;
running = true;
while (running)
{
up = true;
if (value >= 1f)
{
up = false;
value = 1;
lt.intensity = value;
running = false;
}
}
StartCoroutine(day());
}
private void DayToNight()
{
Debug.Log("its dusk");
time = 2;
running = true;
while (running)
{
down = true;
if (value <= 0f)
{
down = false;
value = 0;
lt.intensity = value;
running = false;
}
}
StartCoroutine(night());
}
}
Your code goes into an infinite loop at DayToNight and NightToDay. it gets stuck here:
while (running)
{
up = true;
if (value >= 1f)
{
up = false;
value = 1;
lt.intensity = value;
running = false;
}
}
As you can see, there is no way for value to increase in that loop, and so value will never be greater than one and therefore running will never be false. try do something like value += Time.deltaTime; outside of the if statement, and Unity than never gets to process anything else and therefore crashes. Alternatively, use coroutines as you have been doing.
More formatting
Back to formatting text. If you want, you can learn some html and format the text like that.
Please note that you are less likely to get help if it is difficult to read your code, or if you don't provide enough information. I highly suggest that you read the FAQ on Unity.
Also if this is the answer you need, don't forget to accept it as the answer to show that you are happy and no longer need help.
Hope this helps and Good Luck ~ Jared
Answer by sisse008 · Dec 03, 2017 at 02:26 PM
DO NOT USE WHILE IN UPDATE!! update runs at everyframe so if youre frame rate is 30 your update function will run 30 times in a second (or something like that). there is no While loops in unity because the whole update function is a while loop in it self (while (play)) use if statement instead.