For Loop: Use of Unassigned Local Variables Error

I can’t figure out what the error is. I can comment out the for loops and the rest of the code is fine. But when I put it back the error happens.

Errors:
Use of unassigned local variable x
Use of unassigned local variable y

Here is the erroring code:

for( int x; x < dmgCol.forcesApplied.Count; x++) // Error Here
            {
                for( int y; y < defenseApplied.Count; y++) // Error Here
                {
                    if(dmgCol.forcesApplied[x].forceType + defenseApplied[y].forceType == 0.0f)
                    {
                        temp += dmgCol.forcesApplied[x].forceCur * (defenseApplied[y].forceCur - .05f);
                        Debug.Log(temp);
                       break;
                    }
                    if(y == defenseApplied.Count - 1)
                    {
                        temp = dmgCol.forcesApplied[x].forceCur;
                        Debug.Log(temp);
                    }
                }
            }

You need to assign it a value, in this case probably zero.

It should read:

for (int i = 0; i < blah; i++)

etc.

Oh, right. Wow. I can’t believe I overlooked that.

You’re welcome. Sometimes after you’ve been staring at code too long, it takes a fresh set of eyes to go “Hm, what about that?” :slight_smile:

2 Likes