- Home /
Help! Unsaved work stuck in frozen unity, believed to be from infinite loop. What can i do?
I used this code as i wanted to only move one way at a time to stick strictly to two axis but i believe i created an infinite loop from looking at http://answers.unity3d.com/questions/32826/can-the-unity-editor-break-out-of-an-infinite-loop.html, the question seemed to solve my problem but as i have the latest version of unity i now use visual studio not mono-develop so i am quite stuck. I will display my code so that you can maybe spot how to avoid an infinite loop while keeping the desired functionality.
using UnityEngine;
using System.Collections;
public class Swivel : MonoBehaviour {
public int speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
int blah = 1;
while (blah == 1)
{
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.up * Time.deltaTime * speed);
break;
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(Vector3.down * Time.deltaTime * speed);
break;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Rotate(Vector3.left * Time.deltaTime * speed);
break;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Rotate(Vector3.right * Time.deltaTime * speed);
break;
}
}
}
}
Visual studio is still working but i just need help to protect my unsaved work through the same technique as
I know it's an old question but I do have a better solution. As others have said: don't use an infinite loop by design. However if you did it by mistake and don't want to loose a whole scene or other unsaved data here's what you can do:
1) Open mono develop and attach it to the editor if you haven't already.(Run
Attach To Process)
2) Pause execution from mono develop.(Run > Pause)
3) Go to your immediate window (View > Debug Windows, if you don't see it at the bottom).
4) Modify the value that's causing you to loop infinitely. The Immediate window lets you just type in some code that will execute in that scope.
For me, the problem was looping over a list and trying to find a specific value that matched an element. I didn't properly handle the value not existing so it looped infinitely. All I had to do was call listname.Clear(); in the immediate window and the loop broke out on the next iteration.
If you were doing something exceptionally silly like while(true){ / some silly code / } you might be able to break out by just calling return. If you were calling that function every frame you might have to be a bit more sneaky and call T.Destroy(gameObject.GetComponent()) or something to that effect to remove the script that's calling that silly function.
I hope this helps someone else who doesn't want to nuke their work in progress!
Cheers.
Thank you for reading this and if you can help please help sooner rather than later as I am eager to finish off my work. Thanks!
I am having to turn my computer off so it is gone i believe but still it would be useful to know what was wrong with my code and how to fix it. Thanks
Sorry I don't know about breaking out of such a loop.
But the way to avoid the loop while keeping the (apparently) desired functionality is simply to remove the loop, ie delete these 4 lines...
int blah = 1;
while (blah == 1)
{
}
As it says in your code, Update() is called once every frame. So those conditions are going to be being checked every frame without the loop. All your loop does is keep the flow in that Update function and prevent Unity from getting on with anything else.
I'm afraid you are actually doing what the quote (which is from where?) describes as "something exceptionally silly".
Yes, i guess i did do 'something exceptionally silly' @Bonfire Boy as described in a comment in the answer that i linked
Answer by Owen-Reynolds · Oct 22, 2015 at 07:53 PM
The time-honored, mostly official answer is: Save your work. You have to lose unsaved work several times before you learn to Save, make backups ... and this is one of those times.
A real project involves redoing and rewriting the same code over and over and over as insane managers come and go, testers hate what you have now ... . Having to redo a few days of lost work (which won't take nearly as long the second time) is good practice for this.
Yes, i agree with you, doing it again will not be too bad. I would hope that in the future unity do work out how to stop infinite loops freezing unity, surely there is a way they can be identified and an error message shown. Thanks
Well, sure, freezing everything on an infinite loop isn't something that should happen. And in most editors, it won't happen -- you can press the Stop button, in those. But no one can detect "bad" infinite loops (there are good ones, like Update.)
But it's due the the way Unity is made: being able to pause the game, move things around, and play with the Inspector while it runs. I suspect that pulling the running game apart from the editor (to fix the infinite loop freeze) would be a huge, huge pain, and might cause some features to be cut.
Surprisingly, after the first few times $$anonymous$$e froze, I just got used to this. I'm happy if this is on the bottom of their to-do list.
there is actually a way to unfreeze your code by editing it in debug mode on visual studio. i commented a video that shows it.
Answer by Awesomeking570 · May 13, 2021 at 03:50 PM
https://youtu.be/mL0vyU0H0Jg this video explains in pretty good depth exactly what to do if your unity completely freezes from a loop and you use visual studio
Your answer
Follow this Question
Related Questions
How to handle long loops 3 Answers
Unity Freezing 1 Answer
Mecanim animation Looping problem (Unity 4.6) 0 Answers
How do i make it rain spheres continuously in a loop in Cardboard SDK? 0 Answers
monodevelop vs visual studio? 1 Answer