Dynamic Material & Textures


I need to create a script that creates a new material that assigns itself an image from my resources folder as a texture.

So far i gather that i need to:

function Update () 
var count = 0;
while(count != 4)
  var material = new Material (Shader.Find("Specular"));
  AssetDatabase.CreateAsset(material,("Assets/Resources/tMat" + count + ".mat"));

make a new material, name it procedurally, but im having trouble implementing resources.load to assign the image.

Then i need to instantiate a prefab that has one of these procedurally created materials attached. Lots of trouble with this:

theX = a; //counter to move it
obj = GameObject.Find("arch");
obj.Instantiate(obj, Vector3 (theX, 0, 0), Quaternion.identity); 

I just dont know how to assign a material with

renderer.material.MainTexture = Resources.Load("whatever")

Any pointers would help, im stuck!

I made gist: Create material at runtime

I’ll update it if found something new with this theme.