- Home /
InvalidOperationException: Operation is not valid due to the current state of the object.
Hey guys, I'm trying to get a single sprite from a spritesheet (to use as icons). However, I'm getting an error I havn't encountered before and eventhought alot of the time the errors tell you enough information to fix the error... I have not succeeded. So now I'm asking for your help
Here's my code public void LoadIcons() { // Load all sprites in atlas Sprite[] abilityIconsAtlas = Resources.LoadAll("AbilityIcons");
print (abilityIconsAtlas.Single (s => s.name == abilityName1));
if (abilityIconsAtlas.Single(s => s.name == abilityName1) != null) {
abilityIcon1.sprite = abilityIconsAtlas.Single(s => s.name == abilityName1);
}
if (abilityIconsAtlas.Single(s => s.name == abilityName2) != null) {
abilityIcon2.sprite = abilityIconsAtlas.Single(s => s.name == abilityName2);
}
if (abilityIconsAtlas.Single(s => s.name == abilityName3) != null) {
abilityIcon3.sprite = abilityIconsAtlas.Single(s => s.name == abilityName3);
}
if (abilityIconsAtlas.Single(s => s.name == abilityName4) != null) {
abilityIcon4.sprite = abilityIconsAtlas.Single(s => s.name == abilityName4);
}
}
Here's my error.
InvalidOperationException: Operation is not valid due to the current state of the object
System.Linq.Enumerable.Single[Sprite] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback)
System.Linq.Enumerable.Single[Sprite] (IEnumerable`1 source, System.Func`2 predicate)
Ability_Use.LoadIcons () (at Assets/Scripts/Abilities/Ability_Use.cs:58)
Ability_Use.Start () (at Assets/Scripts/Abilities/Ability_Use.cs:45)
Thank you for your time, Bazza D Lisk.
Answer by xteven9 · Apr 24, 2016 at 05:12 PM
This error means abilityIconsAtlas either has none or multiple elements that matched your abilityName.
See documentation: Enumerable.Single
Your answer
Follow this Question
Related Questions
Normalized UV from batched sprite sheet 0 Answers
error CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `int' 1 Answer
m_TransformInfo.localAABB.IsValid()!!!!!??????? 3 Answers
Heath regain via Pickup code halp me 1 Answer
NullReferenceException: Object reference not set to an instance of an object 1 Answer