This question was
closed Mar 23, 2016 at 05:18 PM by
klisman for the following reason:
The question is answered, right answer was accepted
Question by
klisman · Mar 23, 2016 at 03:47 PM ·
c#buildprogramming
Problem using Linq result - Operation is not valid due to the current state of the object
I'm getting this problem, using Ling filter in a list. bestBoughtUpgrade should be the max upgradeID, what i'm doing wrong?
InvalidOperationException: Operation is not valid due to the current state of the object
System.Linq.Enumerable.Iterate[ModuleUpgrade,Int32] (IEnumerable`1 source, Int32 initValue, System.Func`3 selector)
System.Linq.Enumerable.Max[ModuleUpgrade] (IEnumerable`1 source, System.Func`2 selector)
//Code
public void setMaxUpgrades() {
foreach (Module module in modules) {
int bestBoughtUpgrade = module.modUpgrades.Where(u => u.bought == true).Max(id => id.upgradeID);
playerc.transform.GetChild(1).transform.GetChild(bestBoughtUpgrade).GetComponent<SpriteRenderer>().color = modules[module.moduleID].modUpgrades[bestBoughtUpgrade].upgradeColor;
}
}
Comment
Best Answer
Answer by klisman · Mar 23, 2016 at 05:18 PM
Solved with help of TheOCD in IRC
It's need to check if no upgrade is bought, Max() can't be 0.
Follow this Question
Related Questions
How to convert User Values into degree(Angle)?? 2 Answers
Why does this C# doesnt Work? it seems to be right. 0 Answers
Camera Follow Problem 0 Answers
Trigger events from different sources 0 Answers
Simple question about single or multiple Monobehaviours 1 Answer