Just came across this problem myself, many of the answers were old so will update with my solution that works
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class Mat_library2 : MonoBehaviour
{
[Header("Materials")]
public Material[] myMaterials;
private Material newMaterial;
public GameObject elypseTile;
public int RequiredColorIndex;
public void Start()
{
findAllMaterials();
}
public void findAllMaterials()
{
//find all materials in resources folder Assets>Resources>Materials>
myMaterials = Resources.LoadAll("Materials",
typeof(Material)).Cast<Material>().ToArray();
Debug.Log("Materials " +
Resources.FindObjectsOfTypeAll(typeof(Material)).Length);
foreach (Material mat in myMaterials)
{
Debug.Log("Material is: " + mat);
Debug.Log("RequiredColorIndex: " + RequiredColorIndex);
}
//Apply material from indes to game object
elypseTile.GetComponent<Renderer>().material.color =
myMaterials[RequiredColorIndex].color;
}
}
}