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 Tocaro · May 03, 2013 at 04:48 AM · ifforelse

For loop has 'if-else' impossibility.

An if else statements inside my for loop is giving impossible print readouts.

 void CheckDistance()
     {
         //Gets a list of all the heroes, nulls the ones that are too far away
         //Or are the players hero.
         //And it's really not working.
         Hero = GameObject.FindGameObjectsWithTag("Player");
         bool OneInRange = false;
         if(Hero.Length > 1)
         {
             //Now we have to enabled each icon.
             //And have it follow the appropriate guy...
             
             for(int x = 0; x < Hero.Length; x++)
             {
                 print("HERO CHECK:" + Hero.Length + x + (HeScript.MyHero).name + Hero[x].name);
                 if(Vector3.Distance(Hero[x].transform.position,transform.position) > 4 && gameObject != HeScript.MyHero)
                 {
                     print("HERO FAR");
                 }
                 else
                 {
                     print("HERO Close");
                     //Hero[x] = null;
                 }
                     
             }
         }
     }

But here's what it prints out:

HERO CHECK/ HERO FAR/ HERO CHECK/ HERO CHECK

Which should be impossible since either the if statement is true and it prints HERO FAR or it isn't and it prints HERO CLOSE.

But it somehow prints HERO CHECK and just skips it twice... I even commented out the nulling line and it still did the same thing.

I'm totally baffled, any ideas?

EDIT:

Just added a print statement (Print("END FOR")) to the end of the for loop. Now it reads

HERO CHECK/ HERO FAR/END FOR/ HERO CHECK/ HERO CHECK

So basically it looks like it doing a full loop then... Something that I have no idea about.

Comment
Add comment · Show 3
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 s_guy · May 03, 2013 at 05:09 AM 0
Share

Is it possible that you have this script on more than one object, and so are getting confusing messages?

Have you tried using a breakpoint and stepping through to find a clue?

avatar image Tocaro · May 03, 2013 at 05:57 AM 0
Share

It should only be on one object, since it's essentially the cinematic object (Therefore only one per scene.) But either way, that would just mean it's running the loop the normal number of times * (The number of objects the script is on).

The print output shows the number of runs correctly for just one object, but it entirely disregards the if-then statment for the everything but the first run, and doesn't even print the end statment. It's as if after the first run it just always breaks when it hits the if statment.

avatar image Tocaro · May 03, 2013 at 06:18 AM 0
Share

I checked the scene, there's only one object.

Not sure how to do the breakpoints, I have all the things they say to have check checked, but it doesn't seem to be pausing at any point even after I insert multiple breakpoints.

2 Replies

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

Answer by whydoidoit · May 03, 2013 at 06:33 AM

You have Collapse turned on in the Console I'd bet.

alt text


screen shot 2013-05-03 at 07.33.27.png (7.2 kB)
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 Tocaro · May 03, 2013 at 06:37 AM 0
Share

Oh god. Yep. Somehow I knew I'd look like a total idiot at the end of all this because the logic just couldn't be that fundamentally wrong. I must have accidentally clicked it at some point, never even used it before, don't even know what it does. Blargh.

Thanks so much though, that was driving me crazy!

avatar image whydoidoit · May 03, 2013 at 06:47 AM 0
Share

It's sometimes useful (your log has a statement happening continually so you can't really read anything else etc) I reckon it should change colour when you have it on!

avatar image
0

Answer by DaveA · May 03, 2013 at 06:20 AM

If it should be printing either FAR or CLOSE but prints neither, then it's not getting that far. My guess is it's tossing an exception just before that, line 15, something in there not set right. Set a breakpoint there and examine all the values. My guess is there's an undefined or null in there.

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 Tocaro · May 03, 2013 at 06:31 AM 0
Share

Here's the thing though, I can simplify it to just this:

 for(int x = 0; x < 5; x++)
             {
                 print("HERO CHEC$$anonymous$$:");
                 
                 if(1 == 2)
                 {
                     print("HERO FAR");
                 }
                 else
                 {
                     print("HERO Close");
                     
                 }
                 print("END FOR");
             }

And it still only prints HERO CHEC$$anonymous$$/HERO FAR/HERO CLOSE as if it's only going through the for loop once. I mean that just plain doesn't make sense. The object and script are not destroyed either, because I thought that might be it.

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

14 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

Related Questions

Read Array Inside Array 0 Answers

problem with instance 1 Answer

Both if and else are running, because else condition is met after if 1 Answer

How do I check if all objects in an array are destroyed? 2 Answers

Else and If 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