- Home /
The question is answered, right answer was accepted
Why is unity freezing?
Im trying to make a program so when I press the "D" key the program will subtract one away from "test" and print out "You pressed D". i put this code in to an empty object and when i pressed play unity freezes.
using UnityEngine;
using System.Collections;
public class testScript : MonoBehaviour {
// Update is called once per frame
void Update () {
int test = 10;
do {
if (Input.GetKey (KeyCode.D)) {
--test;
Debug.Log ("You pressed D");
}
} while(test>0);
}
}
Answer by Officerfriendly · Dec 31, 2014 at 05:33 AM
Thank You to all who took time to help me. The problem was the do loop in the update function.
Answer by Dragon-Of-War · Dec 30, 2014 at 04:02 AM
I think i saw your problem. 1) You dont run whiles on a update becouse Update is already running as a loop and really fast. 2) Test will always be setted back 10 when update is done. Try this:
using UnityEngine;
using System.Collections;
public class testScript : MonoBehaviour {
int test = 10;
void Update () {
if (Input.GetKey (KeyCode.D)&&test>0) {
test--;
Debug.Log ("You pressed D");
}
}
}
rather then '--test' you have to write 'test--' in your script try it.
There's no problem using while loops in Update, just don't write an infinite loop.
There's no functional difference between --test and test-- when run by itself.
Follow this Question
Related Questions
While loop freezing program 2 Answers
Unity Games Freeze,Unity Games Freezing 1 Answer
Unity freezes on play after moving project folder 0 Answers
built game freezing 1 Answer
Multithreading freezes editor 1 Answer