how to make an object's colour change when it hits another object?

I want the colour of my object change when it hits another object , and if its possible the result must be the sum of my object’s colour and the object that is hitting it .

Example : if my object’s colour is Red and its hitting a yellow object , then the result colour of my object would be Orange .

Assuming you already have colliders set up this code would do something similar to what you describe.

It relies on the “color” being set by tinting the material, if this assumption is false thing become significantly more complex.

It is creating the mean of the two colors, if you would like true addition then change the arithmetic on the line where we declare newColor

void OnCollisionEnter(Collision col) {
    //find the other objects color
    Renderer otherRenderer = col.transform.renderer;
    if(!otherRenderer) return;

    Material otherMaterial = otherRenderer.material;
    if(!otherMaterial) return;

    Color otherColor = otherMaterial.color;

    //find this objects color
    
    if(!renderer) return;

    Material thisMaterial = renderer.material;
    if(!thisMaterial) return;

    Color thisColor = thisMaterial.color;

    //combine the colors

    Color newColor = (thisColor + otherColor) / 2;

   //apply the new color

    thisMaterial.color = newColor;
}

Thanks everyone ,
But it didn’t work , i put the script on both of the objects and set the colour of them by creating a material but nothings happen when they hit each other .
I’m new in Unity and programming , i’m studying sound design and our task is to create a simple game and mainly work on the sound and music for it , but i really liked to try this idea !

All the best ,
Arash