- Home /
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.
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?
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.
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.
Answer by whydoidoit · May 03, 2013 at 06:33 AM
You have Collapse turned on in the Console I'd bet.
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!
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!
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.
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
Follow this Question
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