How can I assign a Material to a Mesh via code?

I am asking a lot of questions on one topic lately (Mesh in C++), but there is really very little information on how to do basic things like that. I was wondering how I could apply a Material variable to my script, that I can then assign to my Mesh component. Thank you in advance!

If you want to set material from c++ side, there’s SetMaterial method (inherited from UPrimitiveComponent) in UMeshComponent.

Yeah, I read that. Could you give me an example of how to implement it? I am not sure for what I should use as the “ElementIndex”.