public float[] tickValue = new float[6];
public float GetFishPerSec()
{
float tick = 0;
foreach (float item in tickValue)
{
tick += idleCount[0] * tickValue[0];
}
return tick;
}
I think that my issue is the word item not being defined, but i thought that in a foreach loop and an array you set it up this way saying float item in tickvalue. isnt item the slot number in the array? [0] through [5] ? its causing my unity to freeze on test play i think.
error says its assigned but never used… though i thought it was being used in that foreach loop… huh
You define a float item in line 6. Then you never use it. The compiler throws a warning, because its not normal. But technically speaking its not wrong.
This loop on its own will not cause Unity to freeze. A freeze in Unity is caused by an infinite loop. This loop as written is not infinite.
As a general guide this is not really the point of a foreach loop. A foreach loop goes through a collection and does something with every item in the collection. Your code couldn’t care less what is actually in the collection.
A better way would be to use a for loop. An even better way would be to get the math right and calculate this in a single equation.
- foreach (float item in tickValue)
- {
- tick += idleCount[0] * tickValue[0];
- }
Your tickValue should be a list or array, it has multiple items in it, when you do a foreach on it, each loop it will return a value from the list/array, and stored in item. You are supposed to either use that variable “item” or modify your code to not to declare the item.
Technically your entire looping code can be changed to
tick = idleCount[0] * tickValue[0] * tickValue.Count;
1 Like