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][1] and stepping through to find a clue? [1]: http://docs.unity3d.com/Documentation/Manual/Debugger.html
– s_guyIt 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.
– TocaroI 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.
– Tocaro