Change all materials of renderers to one material. And Change back to original Material

I am trying to change the material of a game objects renderers and then revert it back upon task completion but it is not working

MeshRenderer[] mesh = interactObject.GetComponentsInChildren<MeshRenderer>();
List<Material> meshSave;
foreach (MeshRenderer render in mesh)
  {
     meshSave.Add(render.material);
     render.material = interactMaterial;
   }

I get an error on meshSave.Add(render.material); saying that NullReferenceException: Object reference not set to an instance of an object. The code above is setting the original material into a list to then reference again when I want to revert the material back to its original. And then it takes each MeshRenderer and sets it to the new material called “interactMaterial”.

I tested it and the only thing that is not working is saving the original material into the list

YOu ned to actually create the mesh save. Otherwise it is null. For example
List<Material> meshSave = new().
renderer.material refers to MAIN material only. It won’t alter other materials.Use Renderer.materials.

It is uncommon to have more than one MeshRenderer on an object. There’s going to be only one, possibly with multiple materials on it.

1 Like