Change individual colors in objects cloned

rated by 0 users
This post has 1 Reply | 1 Follower

Top 75 Contributor
Posts 35
SunBurn_Community_Licensee
SunBurn_Indie_Licensee
SunBurn_Pro_Licensee
eoigames Posted: 03-11-2010 5:35 AM

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.

Top 10 Contributor
Posts 1,104
SunBurn_Community_Licensee
SunBurn_Contributor
SunBurn_Pro_Licensee

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:

  • Classes are passed by reference
  • Structs are passed by value

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 ;-)

Page 1 of 1 (2 items) | RSS