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 cux.soulhunter · Jun 23, 2011 at 01:18 AM · errorcrashlighteditor-crash

Torch Script Error - Unity crashes

Hello,

I'm trying to write a Torch Script for my game im working on. Everytime I'm loading it into Unity my program crashes.

Do someone could help me out please?

regards,

Soul

Comment
Add comment · Show 3
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 cux.soulhunter · Jun 23, 2011 at 01:19 AM 0
Share

Here's my current Script:

 using UnityEngine;
 
 using System.Collections;
 
 using System.Threading;
 
 public class Torch : $$anonymous$$onoBehaviour {
 
 public int Charge = 100;
 
 public int LightOn = 1;
 
 public int LightOff = 0;
 
 private bool isLightOn = false;
     
 public float timeOn = 0.7f;
 public float timeOff = 0.3f;
 private float changeTime = 0f;
     
 
 void Update () {
 
 if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Q)) {
     isLightOn = !isLightOn;
 }
     if(isLightOn == true) { 
             if(Charge > 0){
             light.intensity = LightOn;
             }
             
             if (Charge < 16){
                       if (Time.time > changeTime) {
                           light.enabled = !light.enabled;
                       if (light.enabled) {
                           changeTime = Time.time + timeOn;
                           } 
                     else {
                           changeTime = Time.time + timeOff;
                           }
                       }
                 
             
                     while (isLightOn == true && Charge > 0){
                         Charge --;
                         Thread.Sleep(1000);
                     }
             
             
             }
     else{
         light.intensity = LightOff;
         }
 }
     while (isLightOn == false){
             Charge ++;
             Thread.Sleep(300);
         }    
     }
 }
avatar image save · Jun 23, 2011 at 01:25 AM 0
Share

Script formatted.

avatar image Bunny83 · Jun 23, 2011 at 03:02 AM 1
Share

When do you think does this while loop end? Right, when isLightOn is true. When does it turn true? Right, never.

     while (isLightOn == false){
         Charge ++;
         Thread.Sleep(300);
     }

This whole piece of code needs some general cleaning like Eric said :D

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Eric5h5 · Jun 23, 2011 at 01:44 AM

You really don't want to use Thread.Sleep in Update; all scripts in Unity run in a single thread. Instead of Update, use coroutines (which will also get rid of most of the spaghetti code). Also, instead of trying to use ints and constant time values, use floats and Time.deltaTime. This way values change every frame, instead of in chunks, which will look smoother. Edit: and what Bunny83 said in the comments about the infinite loop is what's actually causing the crash. :)

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

6 People are following this question.

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

Related Questions

How do I not bake and cluster lights 0 Answers

Unity Editor crashes OSX 4 Answers

Blue screen of Death... 3 Answers

unity 3d freezes 5 Answers

Strange repeating Unity3D crash that occurs at the same event but shows no clear cause! 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