Check on what object I clicked

Hello.
I was wondering how can I check what’s the name of the object i clicked. I almost finished my game and I want to add a new feature. When the user slicks on a place in the map, a new soldier gets created there. I was wondering how can i check on what the user clicked (i want to restrict user from creating a new soldier on a place with water.)

How can I check this. Thank you.

The tag system works pretty good for this feature. Just add it to your conditional statements.

function OnMouseDown() {
if(gameObject.tag == “land”) {
//Do what i want.
}
}

Something like this can help control things.

yes, use tags on the objects in the scene and based on those you can decide if you can do an action

Thank you for your answers but I already know about OnMouseDown function. I was wondering if I could do this from another object (not terrain)

Perhaps you could use the Physics.Raycast method: Unity - Scripting API: Physics.Raycast

Specifically, the last example on that page shows how you can use the current mouse position to cast a ray to see what you hit. The resulting RaycastHit object that’s returned can tell you what object you hit (hit.transform.gameObject.tag) Unity - Scripting API: RaycastHit