- Home /
Change the color of a material for only one object
Is it possible to change the color of an object's material using the editor without changing the color of all other objects using that material?
I mean, using the editor, not via scripting.
Answer by monotoan · Mar 24, 2017 at 12:19 PM
A form of this is possible now using Unity's sparsely-documented but extremely useful "Material Property Block" feature.
You will have to write a script to set yourself up to use this feature, but once that script's written, you can add it to objects and then set their specific color through a color-picker in the component inspector / editor window, much like you would on a material.
Here's a good introduction to this feature: http://thomasmountainborn.com/2016/05/25/materialpropertyblocks/
And here's the official documentation, such as it is: https://docs.unity3d.com/ScriptReference/MaterialPropertyBlock.html
That is working perfectly and exactly what i was looking for. Thanks monotoan.
Answer by Eric5h5 · Mar 06, 2013 at 05:38 AM
No, the color is part of the material, so if you change the color, any object using that material will have the color changed. Technically it's not possible to do in code either; you get the illusion that you can, because Unity creates a copy of the material for you behind the scenes (which can cause issues in certain circumstances, so I'm not sure that was the best design choice). So anyway, if you want two objects to have two different colors, you need two different materials.
Answer by metalvblue · Jul 30, 2019 at 07:46 PM
Best way is to : this.gameObject.GetComponent().material = Instantiate(Resources.Load("Material") as Material);
This way it becomes an instance material during runtime therefore seperate and unique only to that gameobject, then you can just change the tint color or color for example:
this.gameObject.GetComponent().material.color = Color.cyan;
or
this.gameObject.GetComponent().material.SetColor("_TintColor", Color.green);
Yessssss, totally forgot about this. Lifesaver! Also, if you prefab something and want to keep the prefab instance in tact, this is clearly the best answer!
Answer by paranoidray · Dec 02, 2015 at 09:47 AM
The best way to do this is to copy the existing Material ( Ctrl-D ) and then change the color on that new Material.
And then in the code or in PlayMaker you can assign the new Material, that works much better and leaves other instances alone.
Answer by pixels3d · Oct 09, 2013 at 12:32 PM
you attach a script to your game object, you create a public color variable, then in your update or start method you write this gameObject.renderer.material.color = your_color_variable ;