I got this code from somewhere else.
foreach(Renderer r in target.parent.GetComponentsInChildren()) {
foreach(Material M in r.materials) {
M.color = setColor;
}
}
How do I make it so it only changes color to a certain material?
The material name is “Primary Color”, How do I refer to them…?
can I use string? or should I use an Index?.
If your script is on a gameobject, you could just make a material property and drag in the material reference in the inspector and then check in your loop if the material in the renderer is the same material.
Example:
public Material material;
public void YourFunction() {
foreach(Renderer r in target.parent.GetComponentsInChildren()) {
foreach(Material M in r.materials) {
if(M == material) {
M.color = setColor;
}
}
}
}
Edit: Actually, if they use the same material, you only need to change the color of the reference:
public Material material;
public void YourFunction() {
material.color = setColor;
}
Now, if they don’t use the exact same material and rather its own material instance based on the same material; then I would check for the name of the material instead.