How to make a build outline for placing items?

So I’ve been playing Rust lately and I’m currently working on a survival game. One thing I noticed is that when you’re placing your foundations and walls and whatnot in Rust, there’s this green/red outline that shows you where you’re gonna place the foundation that stays level on the terrain. I was wondering how you could go about creating this? Here are some pictures:

Rust (Placing foundation):

Here’s a gif of what I mean if that is still confusing:

Sry for my english
I would make one script wich would work as followed. I give an Example in c#.
On start your mesh should be green “gameObject.GetComponent().SetColor (Color.Green);”
If your mesh collides with anything “void OnCollisionEnter(Collision col)” your mesh gets the color red
“gameObject.GetComponent().SetColor (Color.Red);”
If it leaves the Collision “void OnCollisionExit(Collision col)”
yout mesh gets green again. Of course u want to make a variable wich allowes to build something. So if it is green , a boolean called for example build will be set to true.

the thing that is green is a seperate game Object. when active it is just scriped to moved to the mouse position/select position. they are not changing anything about the terrain. looks like they set the transparency to half to make it look cool and show some grass behind it. when its not in use its transparency is scriped to zero, or its position is simply scripted off screen. indicators are used in most games. this one looks really cool but they all work the same way