x


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;
        }
    } 
more ▼

asked Jul 17 '12 at 08:31 PM

Mattchuuu gravatar image

Mattchuuu
94 9 16 20

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
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()

more ▼

answered Jul 17 '12 at 08:58 PM

Linus gravatar image

Linus
302 1 3 4

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!

Jul 17 '12 at 09:29 PM Mattchuuu

No problem, knew you would figure the rest

Jul 18 '12 at 08:39 AM Linus
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 17 '12 at 09:02 PM

Muuskii gravatar image

Muuskii
1.1k 4 10

My 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.

Jul 17 '12 at 09:32 PM Mattchuuu

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.

Jul 17 '12 at 09:39 PM Muuskii
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1528
x295
x60

asked: Jul 17 '12 at 08:31 PM

Seen: 545 times

Last Updated: Jul 18 '12 at 08:39 AM