Legacy Documentation: Version 5.1
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

MaterialPropertyBlock.SetColor

Switch to Manual
public function SetColor(name: string, value: Color): void;
public function SetColor(nameID: int, value: Color): void;

Parameters

Description

Set a color property.

Adds a property to the block. If a color property with the given name already exists, the old value is replaced.

#pragma strict
// Draws 3 meshes with the same material but with different colors.
public var mesh: Mesh;
public var material: Material;
private var block: MaterialPropertyBlock;
function Start() {
	block = new MaterialPropertyBlock();
}
function Update() {
	// red mesh
	block.SetColor("_Color", Color.red);
	Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);
	// green mesh
	block.SetColor("_Color", Color.green);
	Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
	// blue mesh
	block.SetColor("_Color", Color.blue);
	Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
}

Function variant that takes nameID is faster. If you are changing properties with the same name repeatedly, use Shader.PropertyToID to get unique identifier for the name, and pass the identifier to SetColor.

#pragma strict
// Draws 3 meshes with the same material but with different colors.
public var mesh: Mesh;
public var material: Material;
private var block: MaterialPropertyBlock;
private var colorID: int;
function Start() {
	block = new MaterialPropertyBlock();
	colorID = Shader.PropertyToID("_Color");
}
function Update() {
	// red mesh
	block.SetColor(colorID, Color.red);
	Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);
	// green mesh
	block.SetColor(colorID, Color.green);
	Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
	// blue mesh
	block.SetColor(colorID, Color.blue);
	Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
}