- Home /
Different shader parameters for instances of a material
We have multiple instances of a vehicle prefab in our scene (set up very similarly to the vehicle tutorial), and of course each of these share materials, being based on the same prefab.
As in the vehicle tutorial, the brake light system currently sets and intensity parameter on the shader set on the lights material in the OnUpdate function:
if (controls.ThrottleInput < 0.0f)
{
brakeLights.SetFloat("_Intensity", Mathf.Abs(1.0f));
}
else
{
brakeLights.SetFloat("_Intensity", 0.0f);
}
Unfortunately, since the OnUpdate is called per vehicle, and the shader parameters shared across all instances since they use the same meterial, by the time the object is rendered, the parameter is set to the last calculated value. Is there a way to either:
- Set shader parameters differently on material per object that uses them (having a different material for this makes no sense, defeats the point of a prefab) or..
- Specify shader parameters before each object is rendered, basically a sort of OnPreRender but at object scope rather than camera scope?
Thanks :)
Answer by Jessy · Nov 30, 2010 at 08:53 AM
Don't store brakeLights as a reference to a material. Just store a reference to the Renderer, and use Renderer.material.SetFloat on that. That will automatically instance the materials. (The alternative to Renderer.material is Renderer.sharedMaterial, which would yield the same results as storing a reference to a Material, as you did.)
Thanks so much. Just to complete this, in our case we ended up exposing a Renderer property that artists could set, as well as an index into the materials array to deter$$anonymous$$e which material should have the parameter set: BrakeLights$$anonymous$$esh.materials[BrakeLights$$anonymous$$aterialIndex].SetFloat("_Intensity", 1.0f);
also there is sharedmaterial , some kind of array,in the reference!
Your answer
![](https://koobas.hobune.stream/wayback/20220613085050im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
make unique shader for prefab 1 Answer
Materials showing up black on non-static objects 0 Answers
using shader keywords to toggle pass 0 Answers