Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mattchuuu · Jul 17, 2012 at 08:31 PM · raycastloopwhile

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;
         }
     } 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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()

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mattchuuu · Jul 17, 2012 at 09:29 PM 0
Share

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!

avatar image Linus · Jul 18, 2012 at 08:39 AM 0
Share

No problem, knew you would figure the rest

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mattchuuu · Jul 17, 2012 at 09:32 PM 0
Share

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

avatar image Muuskii · Jul 17, 2012 at 09:39 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges