Reputation: 683
In Unity, we can get the material that the GameObject has with the following code.
Material myMaterial = GetComponent<Renderer>().material;
But with the above code, we can only get one material per GameObject.
However, in actually, Unity GameObjects can have more than one material.
There can be more than one material per GameObject if it is assigned on a face-by-face basis.
I tried to get multiple materials with the code below but it didn't work.
List<Material> myMaterials = GetComponent<Renderer>().material;
Is there a way to get multiple materials assigned to a GameObject?
Upvotes: 2
Views: 18085
Reputation: 21
Material[] myMaterials = thisObject.gameObject.GetComponent<Renderer>().materials;
You can't use ToList
with materials.
Upvotes: -1
Reputation: 26362
You can use the Renderer.Materials
: https://docs.unity3d.com/ScriptReference/Renderer-materials.html
List<Material> myMaterials = GetComponent<Renderer>().materials.ToList();
Upvotes: 5