Reassigning Materials at Runtime

Hi there,

In an OnClick-Function of a GameObject I want to change the material of an object. The material is residing in my Resources folder. Using

Material mat = Resources.Load("SR_Material/Building/SR_Building1_Ghost", typeof(Material)) as Material;
renderer.material=mat;

doesn’t work. Just changing e.g. the materials color works just fine, but I don’t know how to exchange the entire material at once. I also tried copying an existing material of another GameObject without success. Also having a public material on my script and use that one didn’t work either, so I guess I am doing something fundamentally wrong by just assigning “renderer.material=mat”?

Is there anything special I have to do when assigning materials at runtime?

Thanks for the help …

Fairly straight forward script… Maybe there’s a problem with the path for the material.
Keep in mind that all the resources you load with Resources.Load have to be under the Resources folder in your project.

So in your example, you’ll need a folder called Resources and in it a folder called SR_Material and in it a folder called Building and in it the material.

See this post for more info: