- Home /
how to change material color direct from the material
Hi guys ,I need to change material`s color of the object that share with the same material,thx.
Answer by Sonu-Modi · Jun 19, 2017 at 05:54 AM
Make a Public Material variable, in the inspector assign the material... and in the code... Use xyzMaterial.color = whatEverColorYouWant;
You can also try Renderer.Material.color
thanks your reply ,the question is ,i just want to change the material`s color of the object that already assigned. if use this code "rend = GetComponent();",it just change the individual material`s color of the object.i just want to change all the material`s color of the object that share with the same material
Answer by bobisgod234 · Jun 19, 2017 at 05:54 AM
https://docs.unity3d.com/ScriptReference/Material-color.html
If you have multiple objects that use the same material, that will change all the objects materials.
Answer by ifurkend · Jun 19, 2017 at 08:39 AM
To change a material color without affecting other instance like particle system or trail renderer, you need to assign your color to the mesh vertices. Any shader which calls the "COLOR" semantic (deliver color value from each mesh vertex of object component) to multiply the resultant color like all shaders under the (Mobile/)Particles category do. Obviously it would be extravagant to use particle system for merely changing mesh color, so you need a script which changes the mesh vertex color within the same object. The following code is modified from the official API of Mesh.colors:
using UnityEngine;
using System.Collections;
public class meshVertexColor : MonoBehaviour {
public Color color;
Mesh mesh;
Vector3[] vertexArray;
Color[] colorArray;
void Start () {
mesh = GetComponent<MeshFilter>().mesh;
vertexArray = mesh.vertices;
colorArray = new Color[vertexArray.Length];
for (int i = 0; i < vertexArray.Length; i++)
colorArray[i] = color;
mesh.colors = colorArray;
}
void Update () {
}
}
If you want to update color during runtime, move the for loop and mesh.colors to Update function. Most non-particle shaders do not use COLOR semantic to multiply albedo color, you will need to customize your own shader for that purpose. For most shaders adding COLOR semantic is very straightforward even for rookie. Standard Shader is bit tricky but currently there is an official beta Standard Particle Shader (which uses COLOR semantic) for testing.
Your answer
![](https://koobas.hobune.stream/wayback/20220612124439im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing material Color using RBG? 3 Answers
Having trouble with renderer.material 1 Answer
How to change multiple objects' material (color) with one button? 2 Answers
ExecuteInEditMode doesn't work on instantiated list of objects? 1 Answer
HDRP has no Emission effect,HDRP Setting emission has no effect 0 Answers