Question by
Adrianxsa · Aug 30, 2016 at 02:22 PM ·
nullreferenceexceptionforeach
Null Reference Problems with Foreach
Hi, im a newbie C# programer and I have a problem with Null Reference. These are the lines with the problem.
foreach (var EvilElf in FindObjectsOfType (typeof (Animator)) as Animator[]) {
if (EvilElf.runtimeAnimatorController.name == "Flyelf2_0") {
EvilElf.GetComponent <Animator> ().runtimeAnimatorController = EvilFlyelf;
}
}
I dont know why in other Scene works perfectly, and in a new scenes no.
Comment
Answer by Adrianxsa · Aug 30, 2016 at 04:00 PM
I try to write different;
foreach (var EvilElf in FindObjectsOfType (typeof (SpriteRenderer)) as SpriteRenderer[]) {
if (EvilElf .sprite.name == "Flyelf2_0" || EvilForm.sprite.name == "Flyelf2_1") {
EvilElf .GetComponent <Animator> ().runtimeAnimatorController = EvilFlyelf;
}
}
I search for all sprites in the animation instead the Animator.name and it works, but is not a real solution anyway...
Your answer
Follow this Question
Related Questions
Null Reference in script after having called other class without a null reference 0 Answers
Null Reference Exception when changing material shader 0 Answers
If foreach loop isn't recommended,which container should I use to store unsorted data? 1 Answer
How can I simplify a nested foreach 1 Answer
[Help] I don't know how to create a stacking system for my inventory! 3 Answers