Legacy Documentation: Version 4.6.2
Language: English
  • C#
  • JS
  • Boo

Script language

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

MaterialPropertyBlock.SetColor

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public function SetColor(name: string, value: Color): void;
public void SetColor(string name, Color value);
public def SetColor(name as string, value as Color) as void
public function SetColor(nameID: int, value: Color): void;
public void SetColor(int nameID, Color value);
public def SetColor(nameID as int, value as Color) as void

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.

	// Draws 3 meshes with the same material but with different colors.

var mesh : Mesh; var material : Material; private var block : MaterialPropertyBlock; function Start() { block = new MaterialPropertyBlock(); }

function Update() { // red mesh block.SetColor("_Color", Color.red); Graphics.DrawMesh(mesh, Vector3(0,0,0), Quaternion.identity, material, 0, null, 0, block);

// green mesh block.SetColor("_Color", Color.green); Graphics.DrawMesh(mesh, Vector3(5,0,0), Quaternion.identity, material, 0, null, 0, block);

// blue mesh block.SetColor("_Color", Color.blue); Graphics.DrawMesh(mesh, Vector3(-5,0,0), Quaternion.identity, material, 0, null, 0, block); }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Mesh mesh;
    public Material material;
    private MaterialPropertyBlock block;
    void Start() {
        block = new MaterialPropertyBlock();
    }
    void Update() {
        block.SetColor("_Color", Color.red);
        Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);
        block.SetColor("_Color", Color.green);
        Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
        block.SetColor("_Color", Color.blue);
        Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public mesh as Mesh

	public material as Material

	private block as MaterialPropertyBlock

	def Start() as void:
		block = MaterialPropertyBlock()

	def Update() as void:
		block.SetColor('_Color', Color.red)
		Graphics.DrawMesh(mesh, Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block)
		block.SetColor('_Color', Color.green)
		Graphics.DrawMesh(mesh, Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block)
		block.SetColor('_Color', Color.blue)
		Graphics.DrawMesh(mesh, Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block)

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

	// Draws 3 meshes with the same material but with different colors.

var mesh : Mesh; 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, Vector3(0,0,0), Quaternion.identity, material, 0, null, 0, block);

// green mesh block.SetColor(colorID, Color.green); Graphics.DrawMesh(mesh, Vector3(5,0,0), Quaternion.identity, material, 0, null, 0, block);

// blue mesh block.SetColor(colorID, Color.blue); Graphics.DrawMesh(mesh, Vector3(-5,0,0), Quaternion.identity, material, 0, null, 0, block); }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Mesh mesh;
    public Material material;
    private MaterialPropertyBlock block;
    private int colorID;
    void Start() {
        block = new MaterialPropertyBlock();
        colorID = Shader.PropertyToID("_Color");
    }
    void Update() {
        block.SetColor(colorID, Color.red);
        Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);
        block.SetColor(colorID, Color.green);
        Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
        block.SetColor(colorID, Color.blue);
        Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public mesh as Mesh

	public material as Material

	private block as MaterialPropertyBlock

	private colorID as int

	def Start() as void:
		block = MaterialPropertyBlock()
		colorID = Shader.PropertyToID('_Color')

	def Update() as void:
		block.SetColor(colorID, Color.red)
		Graphics.DrawMesh(mesh, Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block)
		block.SetColor(colorID, Color.green)
		Graphics.DrawMesh(mesh, Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block)
		block.SetColor(colorID, Color.blue)
		Graphics.DrawMesh(mesh, Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block)