How do you set a material in c# script?

I would like to know how to set an object’s (a cube to be exact) material to a pre-existing material, specifically the grid material? And also, how do you store an image in a variable to modify the texture? I’ve looked all over and everyone’s examples just use a var Texture; but no clue how to get the texture image into that variable. Any help would be appreciated.

To store an image in a Texture2D, you can use the function : Resources.Load(“imagePath”,Texture2d);

Here the manual : Unity - Scripting API: Resources.Load

Hope it helps !