I haven't done much with XNA but this is a basic C# question.
As the error says, you're trying to iterate over the Effects
of your mesh
but you're casting them all to BasicEffect
instances. BasicEffect
is a subclass of Effect
and not all of the effects that you're adding are of type BasicEffect
. So the cast fails.
Ideally, you'd set the properties of the BasicEffect
objects before they're added, rather than iterating over the Effects
property, but without knowing any more about your code, the best I can suggest would be to do something like this:
foreach (Effect effect in mesh.Effects)
{
var basicEffect = effect as BasicEffect;
if (basicEffect != null)
{
// Do some stuff with basicEffect
}
}
Normally this kind of downcasting is indicative of some flaw elsewhere in your code structure but it may be the best you can do in your scenario (and it's impossible to suggest anything better without a deeper understanding of your code).