Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to set material using script ?

Discussion in 'Scripting' started by shantanusingh994, Dec 28, 2015.

  1. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
    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 -

    Code (CSharp):
    1. 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
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
    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 -
    Code (CSharp):
    1. MeshRenderer mesh = gameObject.GetComponent<MeshRenderer>();
    2.         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
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  5. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
    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

    Code (CSharp):
    1. MeshRenderer mesh = gameObject.GetComponent<MeshRenderer>();
    2.         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 ?
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    How many materials has your mesh renderer? Did you try that you are replacing the correct one at runtime?
     
  7. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
    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
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Do you want to change the albedo texture at runtime?
     
  9. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
  10. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  11. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
    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.
     
  12. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
    ok problem is solved with the use of

    Code (CSharp):
    1. MeshRenderer mesh = gameObject.GetComponent<MeshRenderer>();
    2.         mesh.material = custommaterials[1];
    3.  
     
    dlstilts and Gedas-S like this.