- Home /
Same material on object without the same texture
Hello,
Is it possible to change a material on an object without changing the original material?
I've got an example. I have multiple objects in my scene using the same material. I want to use this material but without the same texture. I don't want to create 100 materials instances for the different 100 object in my scene.
Is it possible? How I can do that?
Answer by deltamish · Feb 18, 2014 at 03:03 PM
Hi, Thats a quite logical question
Yes thats possible all you have to do is create a simple script that duplicates the materail and assigns it to them
 public Texture TheTexture;
 public string ReferenceField = "_MainTex"; // This is used to set the texture in this case the Main texture.
 
 //Hoping that you are using  
 
 void Start()
 {
   Material mat = new Material(renderer.sharedMaterial);
   mat.SetTexture(ReferenceField,TheTexture);
   renderer.sharedMaterial = mat;
 }
THe ReferenceField is just a string that is used to reference the anme of field declared in the shader
And, implicit in the answer, you have to use code. There's no nice Inspector way.
It's a non-obvious Unity rule that code changes almost always copy the material. You can even use merely renderer.material.mainTexture = hapyCowTex; and Unity will change only your personal material (it actually makes a quick duplicate of it the 1st time you change it.)
Compared to changing the texture in the Inspector, where it's always a link to the single material all 100 objects share. There's no easy Inspector way to say "use material A, but with this change just for me."
Thanks for your clear answer guyz!
But in my case, it's seems a little bit special.
Like you can see on the picture, I'm using a sprite render. The sprite selected in the sprite renderer will be automaticly set in the first field of my shader without change it (in the shader I use [PerRendererData] for the _$$anonymous$$ainTex).
But If I change the bump(normal map) in my shader, it's gonna change it in the shader, so each game object has his own texture but have the same bump/normal map texture, thing I don't want.
So in this context how I can change the normal map in my object without changing it in the original shader?

It's not special. Sure, it's co$$anonymous$$g from a SpriteRenderer, ins$$anonymous$$d of a $$anonymous$$eshRenderer. But CustomSpriteShadow is still the same as any other material: It's merely a link in the Inspector, to the real material in your Project folder (even though it looks like your personal copy.) But most changes using code will magically first create a personal copy.
Answer by SlySIy · Feb 19, 2014 at 01:29 PM
Yep, It's working perfectly! With this I'm able to change the bump texture. Thanks a lot to all of you for your precises answers.
There is my final script:
 using UnityEngine;
 using System.Collections;
 
 public class MaterialManager : MonoBehaviour {
 
     public Texture TheTexture;
     public string ReferenceField = "_BumpMap"; // This is used to set the texture in this case the Bump texture. You can find the correct name in your shader.
 
 //Hoping that you are using
 //Yep, I'm using it deltamish!
 
     void Start()
     {
     Material mat = new Material(renderer.sharedMaterial);
     mat.SetTexture(ReferenceField,TheTexture);
     renderer.sharedMaterial = mat;
     }
 }
Answer by castrojoaquin91 · Jun 04, 2021 at 05:04 PM
Hi there! I know this is super old but If you're using a newer version of Unity there are some little changes to make in the Start method:
  void Start()
  {
  Material mat = new Material(GetComponent<Renderer>().sharedMaterial);
  mat.SetTexture(ReferenceField,TheTexture);
  GetComponent<Renderer>().sharedMaterial = mat;
  }
Hope i can help someone :)
Your answer
 
 
             Follow this Question
Related Questions
Get Asset Preview (3.5) 1 Answer
How to partially reveal texture of a plane or object 1 Answer
Good uv mapping tutorial? 1 Answer
Do material instances require manual cleanup? 1 Answer
Textures have dodgy shading 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                