Changing material colour and instanced materials

Sorry to bother again!

            var arr = this.context.physics.raycast();
            if (arr.length > 0) {
                arr[0].object.getComponent(Renderer).material.color.r = 0;
                arr[0].object.getComponent(Renderer).material.color.g = 0;
                arr[0].object.getComponent(Renderer).material.color.b = 1;

I am changing the colour of the object I am clicking on, and I am noticing that it's applying this change to all objects that share this material. I know that in Unity, they typically give objects instances of materials in runtime to avoid this.

I was wondering if this could be done here? or would I need a separate material for every object?

[Original Post on Discord](

*by user 95852307077406720*

Right that’s differing from Unity and should probably be changed or renamed to make it clearer - but material behaves like sharedMaterial. You currently need to clone the material instance yourself for all materials that you intend to change individually

You can just call renderer.material = renderer.material.clone() once on start

In Unity they create a new material instance every time you use the material getter which is also not great I think since it’s a lot of implicit behaviour. Maybe material should be renamed to sharedMaterial in needle so its clear what to expect. What do you think?

I think that’s a great idea! I think then it would have been a little more clearer when I was debugging. Thanks for your help again!

by user 95852307077406720