Max
Max

Reputation: 151

Adding materials to a list

I am having trouble adding materials to a list. My objects have multiple materials assigned to them and I am adding all of that to a specified list. The error I receive is cannot convert from 'UnityEngine.Material[]' to 'UnityEngine.Material'. What am I doing wrong here?

public GameObject[] myObjs;
private List<Material> myObjs_mats = new List<Material>();

void Start () {
         for(int i = 0; i<myObjs.Length; i++){
             myObjs_mats.Add(myObjs[i].GetComponent<Renderer>().materials);
     }

Upvotes: 1

Views: 506

Answers (1)

Jan K&#246;hler
Jan K&#246;hler

Reputation: 6032

GetComponent<Renderer>().materials returns an array. But since you're using the Add method you can only add one item at a time.

So either call Add in a Loop, or just use AddRange instead of Add.

Upvotes: 4

Related Questions