I have 1000 red spheres rolling with sharedMaterial.
Then 1 of them need to start fading into green over time.
So before I start to change color I need to create new material and assign it to that concrete sphere.
In all next ticks I want to change that assigned material’s color.
I can do it in editor with:
if (!UnityEditor.EditorUtility.IsPersistent(MeshRenderer.sharedMaterial))
How can I get same effect in runtime?
Or what is correct approach here? I don’t want to create 1000 instances of same red material.
Color targetColor;
MeshRenderer renderer;
void Update() {
if (renderer.sharedMaterial.color == targetColor) return;
var material = new Material(renderer.sharedMaterial);
Destroy (renderer.sharedMaterial);
renderer.sharedMaterial = material;
renderer.sharedMaterial.color = Color.Lerp(renderer.sharedMaterial.color, targetColor, Time.deltaTime);
}
This code creates Material every frame.
Also it throws first time on Destroy, because it can’t destroy asset material.
If I don’t create it then I change color over all spheres.
If I use .material then it creates new material also as I got from documentation.