- Home /
Using For Loop index as if statement parameter within the loop
Ok, I am so confused and my brain just might be at the end of its rope. But I have this for loop :
for(int i = 1; i <STDTimeFiller.Count;i++)
{
TimeSpan loopAdd;
GameObject looper = STDTimeFiller[i];
TimeSpan.TryParse(looper.GetComponent<Text>().text, out loopAdd);
projectedDuration = projectedDuration.Add(loopAdd);
if (FindCorrectBay.GetComponent<Logger>().PassedProcessNum == "A1" && i==1)
{
ActualTimeObjects[i].GetComponent<Text>().text = FindCorrectBay.GetComponent<Logger>().starter.Add(projectedDuration).ToString("M-dd HH:mm t");
}
}
And I am basically trying to say that if the current task is A1 and we are on the first iteration of the for loop do my stuff. Luckily, without the i call, this round of iteration will work ok and if you take out i==1 it runs as expected. However, when I need to change what happens at A2, this will not work at all. So basically, I just really need the index to be recognized by my if statement.
Can anyone tell me why it's not being recognized? With the i == 1 it completely skips over the if check and continues right down to the next action.
Thank you!
While off topic for the question, I'm motivated first to point out that arrays start at index of zero, not 1. Since your loop is based on the count of STDTimeFiller.Count, then unless there is some reason you intend to skip element zero, you're missing 1. The loop will not run "Count" times, it will run "Count-1" times. The first entry is zero, so your first pass is zero, not 1. That aside, I must say that confusion stems from your description. I'm not exactly sure what you need from your text, but I'm trying to understand. As fashioned, the "if" content will only fire when "PassedProcessNum" is AT, and i is 1, and under no other circumstance. Is it possible what you're seeing is related to my opening point, and process A1 is only in effect when i is zero ins$$anonymous$$d of 1? There are loosely related possibilities here, too. What if FindCorrectBay doesn't actually have a "Logger", and that GetComponent returns a null? That's not accounted for here. I'd suggest peeling these combined steps from each other to see what's interfering, because from the code no one can really say without having a debugger show us what's under the hood. So, start with something like:
Logger l = FindCorrectBay.GetComponent();
if ( l != null) {...}
This will help show if my suspicion has any relevance. Your "if" will be inside the brackets, executing only if l is not null. That if could be peeled apart, so it's in two stages, to see how these separate tests are interacting:
if (FindCorrectBay.GetComponent<Logger>().PassedProcessNum == "A1")
{
if ( i == 1 ) // might need to be zero?
{
ActualTimeObjects[i].GetComponent<Text>().text = FindCorrectBay.GetComponent<Logger>().starter.Add(projectedDuration).ToString("$$anonymous$$-dd HH:mm t");
}
}
See if that moves you toward figuring this out.
Hey there @JVene ! The reason for skipping index 0 is because STDTimeFiller contains the children of a gameObject where index 0 is the parent and I do not want to change the text of the parent in this case. 1 represents the first child object here. Also, I had also tested with the index being the only parameter and it still wasn't recognizing the index value.
Did you put the check for the index INSIDE the check for "A1" as in the example I posted?
I recommend you print the values of "i" and "FindCorrectBay.GetComponent().PassedProcessNum" before the if statement. $$anonymous$$ost likely they are not both true at the same time.