matching materials on collision

Using the script below I’m able to shoot my “laser” at group of meshes, and each one turns red. Everything works perfectly :slight_smile: Each laser shot is one of six random colors (that information is on a different script attached to the “gun”). My goal is to have the mesh that collides with the laser shot change in into the same material as the laser shot. So when a laser shoots out with a green material, the collided mesh turns green, when a laser with a blue material shoots out the mesh assumes the same blue material and so on. I’m totally stumped on where to start with this. If anyone has any ideas please feel free to share :slight_smile:

#pragma strict

function OnCollisionEnter(collision : Collision) 

{
        for (var contact : ContactPoint in collision.contacts) 

	{
                    
		}
 
        if (collision)
        renderer.material.color = Color.red;
}

Hi Alec,

Collision has a reference to the colliding object built right in.

collision.gameObject

So something like:

renderer.material.color = collision.gameObject.renderer.material.color;

might work for you, barring any typos I made.

Cheers,
Cahman

Thanks man. I didn’t get any errors, but it didn’t change the color. But I’ve got something to work with - thank you!

Hey dude! i took out the “color” from the string and it works amazingly! So the final script looks like this:

#pragma strict

function OnCollisionEnter(collision : Collision) 

{
        for (var contact : ContactPoint in collision.contacts) 

	{
                    
		}
 
        if (collision)
        renderer.material = collision.gameObject.renderer.material;
   
}