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 TheBlip · Aug 18, 2013 at 07:19 PM · raycastcrashloopfreezewhile

Unity crashes when using while

Unity crashes when I trigger this part of the script:

 function AutoHit (){
     script.canmove = false;
     while(target == null){
         if(Input.GetKey(KeyCode.Mouse0)){
             var hit : RaycastHit;
             var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
             Physics.Raycast(ray, hit);
             ObjectHit = hit.collider.gameObject.tag;
             if (hit.rigidbody != null){
                 if (ObjectHit == "Untagged"){
                     script.canmove = true;
                     break;
                 }
                 if (ObjectHit == "Enemy");
                 target = hit.collider.gameObject;
             }
         }
     }
     Instantiate(FireBallPrefab,transform.position,Quaternion.identity);
 }

It's activated when the "q" button in pressed. I'll list the useful local variables here:

 var script = gameObject.GetComponent(Movement);
 var FireBallPrefab : GameObject;
 var target : GameObject;
 var ObjectHit : String;
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
0
Best Answer

Answer by Lovrenc · Aug 18, 2013 at 07:26 PM

Unity doesent crash, you created infinite loop! Your "while" never exits, it just goes on and on and on.

If your AutoHit doesent get target, it will search for it again and again... but nothing in your game really changed (you are searching same data every time!).

How to fix:

Change "while" to "if". If your AutoHit is suppose to find target every time it is called but it doesent, then something else in your code/game design is not working well.

============= EDIT =============

This is how code works.

Lets say you have:

  1. Statement1

  2. Statement2 <---- this is your while loop

  3. Statement3

When you start this program, first Statement1 will fire, when done statemen2 will execute and at the end statement3. Now if you have an infinite loop, program will just stay at Statement2 as it never finnishes and Statement3 will also never execute, also program stops responding.

Therefore you HAVE to use IF. You need your game to progress!

Lets say you are looking for a mushroom in a forrest by a tree. Also, lets say time stopped. You can look by that tree for 100.000.000.000.000.000 times, if it wasnt there the first time, it wont be there any other time.

How to work around this than? Use unity events. Add your code in functions like Update or FixedUpdate.

 var autoHitStarted: bool = false;
 
 function AutoHit (){
   autoHitStarted = true;
 }
 
 
 function Update() {
   if(autoHitStarted) {
     //Find your target
     //if you found target, set autoHitStarted = false;
   }
 }
Comment
Add comment · Show 6 · 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 TheBlip · Aug 18, 2013 at 07:31 PM 0
Share

That's what I want though. I want that part of the script to continuously loop until it gets a target. If I use an "if" statement it won't loop. The data doesn't change until the user clicks, but the user can't click because it crashes. Hence why the target is changed to what the ray hits or the loop breaks.

avatar image TheBlip · Aug 18, 2013 at 07:56 PM 0
Share

If I am to understand correctly, the "while" statement freezes in that part of code until it can continue. This means I cannot cast rays inside while loops because the physics won't allow it?

avatar image Lovrenc · Aug 18, 2013 at 07:59 PM 0
Share

No.

Of course physics allows it, and you are probably casting touzands of rays every second, but you locked the game. Game is not progressing. Time is stopped, nothing is moving, nothing is changing.

Also, this is not unity problem, this is pure lack of program$$anonymous$$g knowledge. Check this

avatar image weenchehawk · Aug 18, 2013 at 08:28 PM 2
Share

You probably want your game to run at many hundreds of frames per second. This means that you can't spend any more than a few milliseconds in any given frame. Any code you write must complete quickly. Your while loop is furiously trying to do something . . . forever until it gets it right. If it never gets it right you won't exit & your game won't progress on to the next frame, and if it's running in your editor your editor will never get an opportunity to resume control.

Please understand that if this sounds mysterious to you you're simply starting at the wrong starting point, take some time to grow your software development skills & then dive in. Unity makes everything wonderfully simple but it can't substitute experience.

Incidentally, Lovrenc has answered your question, provided a solution and tried to explain the context to you. I've only done two of those things, in this forum it's appreciated if you mark one or both of our answers as accepted answers.

avatar image TheBlip · Aug 18, 2013 at 09:00 PM 0
Share

No no, I understand it now. I think I merely misspoke when saying "physics". I didn't mean the physics engine but more... physically it can't happen. I'll probably have the script run another script's update function.

Show more comments
avatar image
1

Answer by weenchehawk · Aug 18, 2013 at 07:28 PM

I expect unity is not crashing, but instead Hanging. This may be because your script will execute forever in that loop while target is null.

Target only gets set when ObjectHit = Enemy. Depending on how quickly it takes to hit an enemy it will continue to execute for ever in this script. Now, in addition, your Physics simulator, main game loop, general game progression is paused because your frame does not progress until your loop exits, that's why unity seems to hang,

Comment
Add comment · Show 3 · 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 TheBlip · Aug 18, 2013 at 07:34 PM 0
Share

Would that still cause the editor to not respond? I press the play button again and it won't stop playing unless I close it in task manager. If this is the case however, I'm supposing the "while" event keeps it stuck in the same frame so it cannot cast the ray?

avatar image Lovrenc · Aug 18, 2013 at 07:53 PM 0
Share

You do not understand. It is not the editor, that is not responding, you are essentially stopping your game.

avatar image weenchehawk · Aug 18, 2013 at 07:56 PM 2
Share

While your game runs in the Editor they both share the same execution thread, if one gets caught in an infinite loop so does the other. Both Lovrenc & I have answered the question correctly, what you need to do is figure out how to make your code only do one iteration of a loop each time it is entered. Lovrenc's suggestion of using if ins$$anonymous$$d of while is a fine suggestion.

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

18 People are following this question.

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

Related Questions

Waiting for a mouse click in a Coroutine 1 Answer

Raycasting freezes the Android device? 2 Answers

While loop crashing Unity 2 Answers

while loops infinitely, I can't tell why. 1 Answer

Unity crashes when the game starts: maybe due to a while loop 1 Answer


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