Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

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

Material.HasProperty

HasProperty(propertyName: string): bool;
bool HasProperty(string propertyName);
def HasProperty(propertyName as string) as bool
HasProperty(nameID: int): bool;
bool HasProperty(int nameID);
def HasProperty(nameID as int) as bool

Description

Checks if material's shader has a property of a given name.

	// Attach this to a gameObject that has a renderer.

if(renderer.material.HasProperty("_Color")) renderer.material.SetColor("_Color",Color.red);

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Example() {
        if (renderer.material.HasProperty("_Color"))
            renderer.material.SetColor("_Color", Color.red);
        
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Example() as void:
		if renderer.material.HasProperty('_Color'):
			renderer.material.SetColor('_Color', Color.red)