- Home /
Set material variable with colliding object's material
Hi, I'm trying to grab the material of an object that is overlapping with my trigger and set that to a public variable on the receiving object. I can grab the object it's self and set a gameobject variable, but I can figure out how to access the material on the other renderer. Here is my code,
//Material Detection System
void OnTriggerEnter(Collider theCollision)
{
if (theCollision.gameObject.tag == "MaterialSwatch")
{
incomingObject = (theCollision.gameObject);
}
}
I've tried a few methods below to grab the material on the incoming object, but I can't get anything to work.
//GetComponent<Renderer>().material.CopyPropertiesFromMaterial(theCollision.gameObject);
//incomingMaterial = theCollision.gameObject.renderer.material;
//incomingMaterial = hit.theCollision.gameObject.renderer.material;
Answer by G4merSylver · Aug 14, 2016 at 04:41 PM
The thing you are looking for is [Renderer.Material][1], either you can directly snatch it out of the MeshRenderer.Material and overwrite your current material (I didnt try it out right now.) or you can create a script reference to the material you use and simply overwrite it by using theCollision.gameobject.getcomponent