- Home /
Question by
bmerritt · Dec 17, 2019 at 05:03 AM ·
physicsmaterial colorinstances
material.color only changing one instance of object?
I am working on a bowling game and I have this code on the pins to color change them when they are being knocked around. However, only one of the 10 pins shows the red color when affected? These are placed in the scene btw, not through code. Any idea why this is happening?
private Rigidbody rb;
private Color ogc;
private Renderer r;
GameObject pinBody;
void Start()
{
rb = GetComponent<Rigidbody>();
pinBody = GameObject.Find("body");
r = pinBody.GetComponent<Renderer>();
ogc = r.material.color;
}
// Update is called once per frame
void FixedUpdate()
{
if (!rb.IsSleeping())
{
r.material.color = Color.red;
}
else
{
r.material.color = ogc;
}
}
Comment
Answer by lgarczyn · Dec 18, 2019 at 01:29 AM
When you use .material
, you force unity to create a unique material just for your object. Use .sharedMaterial
to avoid this.
Your answer
![](https://koobas.hobune.stream/wayback/20220612220257im_/https://answers.unity.com/themes/thub/images/avi.jpg)