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 AmazingMrBrock · Feb 09, 2015 at 10:14 PM · raycastraycasthitrayviewportwhile

[c#] While loop not exiting, I have no idea why

What I'm trying to do is make sure that there isn't already a gameObject in the spot I'm trying to spawn my gameObject.

To the best of my knowledge I've accounted for all possible outcomes and am also using the raycast correctly.

    public void SpawnAttackShip(){
             bool spawned = false;
             while(spawned == false){
                 Vector3 position = new Vector3(Random.Range(screenLeft.x, screenRight.x), screenTop.y, 0);
                 Ray ray = Camera.main.ViewportPointToRay(Camera.main.WorldToViewportPoint(position));
                 RaycastHit hit;
                 if(Physics.Raycast(ray, out hit)){
                     if((hit.transform.tag == "Enemy")){
                         spawned = false;
                     }
                     if((hit.transform.tag == null)){
                         GameObject attackShip = (GameObject)Instantiate(theAttackShip, position, Quaternion.identity);
                         spawned = true;
     
                     }
                 }
                 else{
                     GameObject attackShip = (GameObject)Instantiate(theAttackShip, position, Quaternion.identity);
                     spawned = true;
     
                 }
             }
         }

I've been trying to troubleshoot it but its tough because it makes unity crash in the infinite loop.

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 Baste · Feb 09, 2015 at 11:09 PM

Troubleshooting infinite loops is a bit tricky - the best way is often to either open a debugger, or go for the cheap option:

 int safety = 0;
 while(spawned == false && safety < 100)
     safety++;
     ...
 }

Generally, having an while-loop run until some randomly generated value gets a favorable value is not ideal. In your case, it seems that your raycast is always hitting something with the Enemy tag - which might mean that you've tagged some background with "Enemy", or that your rays are not doing what you think they are. A quick solution (that probably should be in anyways) would be to just give up after a bunch of iterations, and try again later.

Also, please, use while(!spawned) instead of while(spawned == false). It reads better - "I want to run the loop while the ship has not spawned" rather than "I want to run the loop while the ship having spawned is false".

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 Bonfire-Boy · Feb 10, 2015 at 12:01 AM 1
Share

Have to say I generally prefer the == false way of doing it, because it jumps out as a negative condition more easily when scanning code quickly. Not that I'd tell anyone else that it's the right way of doing it. I think this one's a matter of taste.

avatar image DanSuperGP · Feb 10, 2015 at 12:22 AM 1
Share

I love that this answer is the one that got accepted... when I actually found why he was getting an infinite loop. ;)

avatar image VOTRUBEC · Feb 10, 2015 at 01:10 AM 0
Share

This isn't really an "answer". And to tell a novice that !spawned reads better than spawned == false .. come now! I bet you're fun at parties! I personally DO use the shorthand version ("!" ins$$anonymous$$d of "== false"). I could go on about beginners missing the "!", a million ways to skin a cat, the fact that you've assumed English as the way to vocalise the code segment, but I'm not here to start a flame war.

But the worst bit of advice you gave, and the reason why I even commented here, is to $$anonymous$$ch a novice about ways to circumvent correct code, by putting in a "safety net". Ins$$anonymous$$d of finding out WHY the code looped infinitely, your suggestion is to cover it up, and hope it goes away. How many novice programmers are going to start implementing this kind of code in every program, in every loop, they code?

In fairness, the one bit of good advice was to use a debugger. I can't stress that enough.

avatar image
3

Answer by DanSuperGP · Feb 10, 2015 at 12:04 AM

 if((hit.transform.tag == null)){

Tag generally never equals null. If you haven't tagged something it has the tag string "Untagged".

This is why you're getting an infinite loop. Neither of your conditions can ever cause spawned to equal true.

Comment
Add comment · Show 1 · 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 AmazingMrBrock · Feb 10, 2015 at 02:23 AM 0
Share

That was only part of the problem, I didn't have a background and the ray wasn't hitting anything at all. I figured it would just return nothing, but I was totally wrong. In the end I make a background and tagged it background to make everything work properly. I Put the other answer above as the answer because it helped me stop unity from crashing so I could debug the problem myself.

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

21 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 avatar image avatar image avatar image

Related Questions

Raycast returns null for no apparent reason 0 Answers

Need Help with RayCast. No Vector? 1 Answer

Raycast goes through 1 Answer

Shoot off multiple raycasts from 1 object? 2 Answers

set ray.point as x, y and z float 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