Version: 5.4 beta (switch to 5.3)
LanguageEnglish
  • C#
  • JS

Script language

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

Obsolete
Creating materials from shader source string is no longer supported. Use Shader assets instead.

Material Constructor

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

Switch to Manual
public Material(shader: Shader)
public Material(Shader shader);
public Material(source: Material)
public Material(Material source);

Parameters

shader Create a material with a given Shader.
source Create a material by copying all properties from another material.

Description

Create a temporary Material.

If you have a script which implements a custom special effect, you implement all the graphic setup using shaders & materials. Use this function to create a custom shader & material inside your script. After creating the material, use SetColor, SetTexture, SetFloat, SetVector, SetMatrix to populate the shader property values.

See Also: Materials, Shaders.

// Creates a material from shader and texture references.
var shader : Shader;
var texture : Texture;
var color : Color;

function Start() { var rend = GetComponent.<Renderer>(); rend.material = new Material(shader); rend.material.mainTexture = texture; rend.material.color = color; }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Shader shader; public Texture texture; public Color color; void Start() { Renderer rend = GetComponent<Renderer>(); rend.material = new Material(shader); rend.material.mainTexture = texture; rend.material.color = color; } }
// Creates a cube and assigns a material with a builtin Specular shader.
function Start () {
	var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
	var rend = cube.GetComponent.<Renderer>();
	rend.material = new Material(Shader.Find("Specular"));
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Start() { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); Renderer rend = cube.GetComponent<Renderer>(); rend.material = new Material(Shader.Find("Specular")); } }