Check the material in script [C#]

How do i check the material of the, let’s say, cube my player is looking at? i want something to be done if the player clicks some button and looks at a cube with some material.
thanks in advance.

You can get the renderer on your cube, then ask it for the material:

//find the game object using whatever means you like
GameObject target_object = WhateverCodeYouUseToGetTarget();

//get the renderer from the object
MeshRenderer target_renderer = target_object.GetComponent<MeshRenderer>();

//get the material from the renderer
Material target_material = target_renderer.material;

//do something based on the material
if( == "cow")
    //do stuff if pointing at a cow!

That’s a fairly basic example - might be a couple of compile errors cos I don’t have unity to hand!

Note that if you aren’t certain the target object will have a mesh renderer, you should add some checks to make sure it isn’t null.