How to set material using script ?

I’ve an object with mesh renderer attached and I’ve a custom material which I want to set to the material of this mesh using C# script
My script code is -

MeshRenderer mesh = gameObject.GetComponent<MeshRenderer>();

after that I am stuck how to set the material to this mesh. I tried using mesh.renderer and mesh.material but there are no methods (at least I’m not able to figure out)

I want to set the material so I was searching for something like “Element 0” type property to which I can apply my material (which we can without using scripts by simple drag & drop the material to the Element 0 option) but there is no such property or method available. Please help me

There is an array of materials:

MeshRenderer meshRenderer = gameObject.GetComponent <MeshRenderer>();
meshRenderer.materials[0] = theMaterialYouWantToAssign;
meshRenderer.materials[1] = anotherMaterialYouWantToAssign;

ok just one more doubt . How should I initialize the materials[0] ? I’ve some Materials present in my Materials folder in the project pane but none of them is coming when I do this -

MeshRenderer mesh = gameObject.GetComponent<MeshRenderer>();
        mesh.materials[0] = ;

There is a material present in the materials folder named “Roller Ball Material” this is what I want to assign materials[0] to

You can e.g. make it available in the inspector, such that you can drag and drop the material in there:

ok after adding the materials using public Materials[ ] materialsCopy; and then drag,drop the materials

I am getting white colored material on my gameobject instead of the set albedo image

MeshRenderer mesh = gameObject.GetComponent<MeshRenderer>();
        mesh.materials[0] = materialsCopy[0];

in materialsCopy the first element(index 0) material has albedo set to an image with a .jpg extension

Where am I doing wrong ?

How many materials has your mesh renderer? Did you try that you are replacing the correct one at runtime?

mesh renderer has only one material slot and while adding the material at runtime (dag,drop) it is taking the image on itself.

One more thing to clearify - I am getting white color because I’ve set the the color at the right side of albedo to white.

I want to set image on the gameobject instead of color

Do you want to change the albedo texture at runtime?

yes

You have gotten two links to the manual already. It contains a lot of useful information and gives you an overview of what is possible. We are talking about a material, which is documented here:

You want to replace some images in it. If you just have a glance at the page, you should see mainTexture:

So, all the information you needed was already there. Use it!

oh I was thinking that like we can set the texture by materials manually we require material to do so in scripts also, didn’t know about mainTexture

But when adding texture directly with mainTexture the image is not wrapping the sphere properly. Sphere is invisible at the top and bottom part.

ok problem is solved with the use of

MeshRenderer mesh = gameObject.GetComponent<MeshRenderer>();
        mesh.material = custommaterials[1];
2 Likes