Skip to main content
added 595 characters in body
Source Link
Michael
  • 103
  • 3

I have some gameobjects that in some conditions have an additional material added to them programatically at runtime.

I then later want to be able to remove that specific material. How can I identify the material? I can iterate over the materials array but haven't found a way to identify the material. material.name gives the gameObject name not the material itself.

private Material myMaterial;
void Start () {
  myMaterial = (Material)Resources.Load ("Materials/MyMaterial");
}

private void addMaterial(GameObject gameObject) {
  Material[] newMaterialArray = new Material[(gameObject.GetComponent<Renderer>().materials.Length +1)];
  gameObject.GetComponent<Renderer>().materials.CopyTo(newMaterialArray,0);
  newMaterialArray[newMaterialArray.Length - 1] = myMaterial;
  gameObject.GetComponent<Renderer>().materials = newMaterialArray;
}

Currently I am assuming the last material in the array is the one to remove, but this may not always be correct.

I have some gameobjects that in some conditions have an additional material added to them programatically at runtime.

I then later want to be able to remove that specific material. How can I identify the material? I can iterate over the materials array but haven't found a way to identify the material. material.name gives the gameObject name not the material itself.

Currently I am assuming the last material in the array is the one to remove, but this may not always be correct.

I have some gameobjects that in some conditions have an additional material added to them programatically at runtime.

I then later want to be able to remove that specific material. How can I identify the material? I can iterate over the materials array but haven't found a way to identify the material. material.name gives the gameObject name not the material itself.

private Material myMaterial;
void Start () {
  myMaterial = (Material)Resources.Load ("Materials/MyMaterial");
}

private void addMaterial(GameObject gameObject) {
  Material[] newMaterialArray = new Material[(gameObject.GetComponent<Renderer>().materials.Length +1)];
  gameObject.GetComponent<Renderer>().materials.CopyTo(newMaterialArray,0);
  newMaterialArray[newMaterialArray.Length - 1] = myMaterial;
  gameObject.GetComponent<Renderer>().materials = newMaterialArray;
}

Currently I am assuming the last material in the array is the one to remove, but this may not always be correct.

Source Link
Michael
  • 103
  • 3

How Can I programatically identify a Material in Unity?

I have some gameobjects that in some conditions have an additional material added to them programatically at runtime.

I then later want to be able to remove that specific material. How can I identify the material? I can iterate over the materials array but haven't found a way to identify the material. material.name gives the gameObject name not the material itself.

Currently I am assuming the last material in the array is the one to remove, but this may not always be correct.