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 Can the Unity editor break out of an infinite loop in a script? - Questions & Answers - Unity Discussions, 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:
- Open mono develop and attach it to
the editor if you haven’t already.(RunAttach To Process)
Pause execution from mono
develop.(Run > Pause)Go to your immediate window (View >
Debug Windows, if you don’t see it at
the bottom).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!