- Home /
While loop crashing Unity
I'm trying to use a While loop to stop the player when he is looking at a wall, but when the player does look at a wall the game crashes, From what I found on my own, if a while loop doesn't have an exit it will become an infinite loop, which is what crashes the game, I'm assuming that the Else statement I made is not doing anything and that's the problem, however I don't know what to use to replace it.
function Update () {
var HitTheWall = false;
var fwd = transform.TransformDirection (Vector3.forward);
Debug.DrawLine(transform.position, fwd * 100);
if (Physics.Raycast (transform.position, fwd, 10)) {
print ("There is something in front of the object!");
HitTheWall = true;
}
else{
HitTheWall = false;
}
while (HitTheWall){
Click2Move.Speed = 0;
}
}
Answer by Linus · Jul 17, 2012 at 08:58 PM
function Update () {
var HitTheWall = false;
var fwd = transform.TransformDirection (Vector3.forward);
Debug.DrawLine(transform.position, fwd * 100);
if (Physics.Raycast (transform.position, fwd, 10)) {
print ("There is something in front of the object!");
HitTheWall = true;
}
else{
HitTheWall = false;
}
if (HitTheWall){ //change this line
Click2Move.Speed = 0;
}
}
I do not see why you want a while() there, try an if()
If I use an If it will set the speed to 0, and then it will stay 0.
Edit: I went and put in an else that resets the speed and it worked. Thanks!
Answer by Muuskii · Jul 17, 2012 at 09:02 PM
This is unnecessary:
var fwd = transform.TransformDirection (Vector3.forward);
Debug.DrawLine(transform.position, fwd * 100);
You can use:
Debug.DrawLine(transform.position, transform.forward * 100);
You don't need a while loop to change a speed value to zero, you set it to zero once and the object should stop moving, in fact take out
HitTheWall = true;
and replace it with
Click2Move.Speed = 0;
It sounds like you're trying to implement collision detection before you have a basic understanding of how program flow works. Please start by doing some programming tutorials before trying game development, it will save you alot of frustration.
$$anonymous$$y problem is that if i just set it 0, it stays at 0 permanently. I only want the speed to be stopped while it's looking at the wall. I wanted to try using a while loop because then by my understand, while the boolean is true it'll set the speed to 0, and when it's not it'll set it back to normal.
However you haven't posted any code that sets speed to anything but zero.
Even setting the speed to something besides zero just before the code you posted will give you exactly what you need, because speed will be set and then your code does it's check which will zero out speed if-and-only-if we hit a wall.
Like I said, you're having program flow issues that would be resolved with basic tutorials. Have you for instance done "Hello World" tutorials for any language before? That sort of stuff prepares your sense of logic for more advanced projects.
Your answer
Follow this Question
Related Questions
Waiting for a mouse click in a Coroutine 1 Answer
Unity crashes when using while 2 Answers
Changing a string in a loop each second 3 Answers
while loop & array- turn on something via percentage 1 Answer
Coroutine While loop help 2 Answers