C# 'a' conflicts with a declaration in a child block

Hi everyone I have a script where I’m trying to use one integer for two for loops. But I keep getting this error ‘a’ conflicts with a declaration in a child block. Any idea how to fix this?

public int a;
public string[] ExampleArray;
public int[] ExampleArray2;
void Start(){
//'a' conflicts with a declaration in a child block
for ( a = 0; a < ExampleArray.Length; a++)
//'a' conflicts with a declaration in a child block
for ( a = 0; a < ExampleArray2.Length; a++)
}

Ok just for thoroughness sake. I tried to reproduce the exact error you were getting. The only way I could reproduce this error was in the following code example.

public int a;
	public string[] ExampleArray;
	public int[] ExampleArray2;
    void Start () {
		for ( a = 0; a < ExampleArray.Length; a++)
		{
			//'a' conflicts with a declaration in a child block
			for ( int a = 0; a < ExampleArray2.Length; a++)
			{
				
			}
		}
		
    }

To me it appears obvious why you would be getting this error because you are redeclaring a in the second for loop. If you declared int the first for loop it would consider it a local variable and all would be fine. So I am assuming since you are not experiencing the problem anymore that what you were trying to accomplish didn’t require this kind of logic and you are good to go. Hope this helps others in the future.

It’s pretty obvious what the problem is. You can’t use the same variable name more than once. ‘a’ is defined in three places, so change the variable names and your error will go away.

public int someNumber;
public string[] exampleArray;
public int[] exampleArrayTwo;

void Start()
{
    for (ia = 0; ia < exampleArray.Length; ia++)
    {
        for (ib = 0; ib < exampleArrayTwo.Length; ib++)
        {
            // something with ia and ib here
        }
    }
}