This is a very confusing question. So, I'll just comment only on what I understand, or assume to understand. Maybe it will help clarify things.
First of all: It seems that you want to use value1, which you have assigned the value of 1, in the foreach loop. You can't do that. Although it's possible, is not good practice, and it won't do what you want it to do. So to clarify: The local variable declared in the foreach loop is just a placeholder, so that you iterate through all the elements of the enumerator, and use the value of the placeholder, in each iteration, to do something with it.
So, in this particular case, in line 11 of your code, you are saying: Iterate through all the elements of array1, and each time round, put the value of the current element in the local variable named value1. This local variable value1, has the same name as the field value1 = 1, declared in line 1. Not a good practice to have variable with the same name, even in different scope.
Bow, having said that, Here's what you can do to answer your question "Check for a specific integer inside of an array and calculate how many times that integer appears in said array".
I assume that you want to check how many times value1 (that is equal to 1) appears in the array1.
int value1 = 1;
int[] array1;
void Start()
{
array1 = new int[5]; //or you could say in line 2, int[] array1 = new int[5]
}
int CountOccurrences(int valueToCheck) //set valueToCheck = value1 or anything else you want, when calling the method
{
int counter = 0;
foreach(int currentValue in array1)
{
if(currentValue = valueToCheck)
{
counter++; //increase counter
}
}
return counter;
}
↧