Hi guys ,I need to change material`s color of the object that share with the same material,thx.
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
If you have multiple objects that use the same material, that will change all the objects materials.
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][1]:
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 *= 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][2] (which uses COLOR semantic) for testing.
[1]: Unity - Scripting API: Mesh.colors
[2]: https://forum.unity3d.com/threads/release-standard-particle-shader.461938/