How to make a "if pointed to" command.

how do I implement a command in a script.
to do something when pointed to a certain object.
Example :
if(gameObject.active){
Destroy(gameObject);
}
I just need an if command.

You might wanna check raycast

look at this example