Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tuocs · Dec 03, 2017 at 07:05 AM · crashfreeze

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()); } },

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pako · Dec 03, 2017 at 09:41 AM 1
Share

In the future, please make an effort to follow the FAQ before posting a question, otherwise your post may be rejected.

Frequently Asked Questions

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Editor Hangs after first play using plugin DLL 2 Answers

Unity randomly going unresponsive when importing script changes 0 Answers

Xbox UWP Game Crashing Upon Loading 0 Answers

How can I find editor log file? 5 Answers

Unity crashes when the game starts: maybe due to a while loop 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges