- Home /
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
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);
}
}
}
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
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. :)
Your answer
Follow this Question
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