Hello.I know that is a simple question, but I cannot find the solution. Well, I have some cloned spheres, and I want to change the color of each model. When I try to do this, all the spheres gets the same color...Here's my code:
if (sphere[1].SceneObject.RenderableMeshes[0].Effect is DeferredObjectEffect) { DeferredObjectEffect newcolor = sphere[1].SceneObject.RenderableMeshes[0].Effect as DeferredObjectEffect; newcolor.DiffuseColor = new Vector3(1, 0, 0); }
What I'm doing wrong? Thanks.
I'm not totally sure because I don't see the rest of your code but it seems to be a reference issue.
When you call:
DeferredObjectEffect newcolor = sphere[1].SceneObject.RenderableMeshes[0].Effect as DeferredObjectEffect;
You are actually creating a reference to the Sphere effect and all changes will be applied to this one as you would expect :)
However, if previously in your code, you initialized your Sphere effects using a unique DeferredObjectEffect such as:
DeferredObjectEffect myEffect = // Whatever you've done to create your effect and changes its properties...
for(int i = 0;i < sphere.Count; i++) // I assume sphere is a collection{for(int j = 0;j < sphere[i].SceneObject.RenderableMeshes.Count;j++){sphere[i].SceneObject.RenderableMeshes[j].Effect = myEffect;}}
Therefore, the newColor effect you referred is actually the same instance as myEffect in my above code which means it is unique.
Remember that in C# and in Object Oriented languages:
And Effect is a class in the Xna framework. ;)
If that is your case, consider using the Clone() Method to initialize your Sphere effects to get the expected result.
Now, it may also be something else ;-)