Home > database >  Adding materials to a list
Adding materials to a list

Time:10-04

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);
     }

CodePudding user response:

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.

  • Related