- Home /
Changing material color from dictionary (C#)
I've been working on an application in which the user loads a 3D model representing a part (important for context as I delve into the situation) into the environment and has the ability to adjust the transparency of that model. I have a working solution for this currently:
Material partMat =
app.currentPart.GetComponent<MeshRenderer>().material;
Color partColor = partMat.color;
partColor.a = val;
partMat.color = partColor;
This solution loads the GameObject containing the model ( app.currentPart
) and successfully changes its alpha channel to val
, the parameter of the slider callback method from which this code was taken.
However, I have also recently implemented a change to my application such that the part can be broken up into its features, each represented by a GameObject containing a 3D model of its own (each such object is a sibling of one another under an empty GameObject). The only real difference in structure is that rather than referring to a sole GameObject app.currentPart
, I refer to app.currentFeatures
, which is a Dictionary<string, GameObject>
. I want to adjust the transparency the same way. Some Googling led me to approach it this way:
List<string> keys = new List<string>(app.currentFeatures.Keys);
foreach (string featName in keys)
{
Material featMat =
app.currentFeatures[featName].GetComponent<MeshRenderer>().material;
Color featColor = featMat.color;
featColor.a = val;
featMat.color = featColor;
app.currentFeatures[featName].GetComponent<MeshRenderer>().material = featMat;
}
However, this yields no changes to the transparency of these models at runtime. I know that app.currentFeatures
is correctly populated because if I print featName
each iteration, it is correct. My suspicion is that this code is editing a copy of the GameObject rather than the one loaded into the environment, but I feel like I've tried pretty much all the different angles for getting the object itself.
Since submitting this question for moderation, I tried another approach based on the object hierarchy rather than my script variables/containers:
foreach (MeshRenderer featMesh in app.partContainer.GetComponentsInChildren<MeshRenderer>())
{
Material featMat = featMesh.material;
Color featColor = featMat.color;
featColor.a = val;
featMat.color = featColor;
}
My thought was that referring directly to the children of app.partContainer
(the empty GameObject holding the part/features as children) would solve this problem, but unfortunately, it did not.
UPDATE: I have since changed the specifications of my application significantly such that each feature has a type and each type corresponds to a uniquely colored material. When I implemented this, suddenly the problem went away entirely. I still would like to see this original question answered just in case others run into the same issue, though.
ok, so at first it was app.currentPart with a $$anonymous$$eshRenderer and just one $$anonymous$$esh, right? Then you split that up and placed each individual GameObject with its $$anonymous$$eshRenderer part into your app.currentFeatures Dictionary along with a name, ok? Then you ran a foreach loop over app.partContainer, asking for its child $$anonymous$$eshRenderers.
So my question is, did either iterations (dictionary or children $$anonymous$$eshRenderer) iterate the $$anonymous$$eshRenderers? Did the code that should reassign the materials execute?
Yes, the code definitely executed during each iteration, and it iterated correctly as well (I had it print out the name of the object containing the $$anonymous$$eshRenderer).
then either all materials changed to the same alpha, the material read did not make use of an alpha, why it stayed opaque, or I don't know.
you said you provided your own set of materials. how do those compare to the ones already applied?
Your answer
Follow this Question
Related Questions
Create Staining effect 0 Answers
Multiple Cars not working 1 Answer
How to make enemies flash on hit 2 Answers
Distribute terrain in zones 3 Answers
Unity3D -Trying to decrease color values when it equals to zero and curious result 0 Answers