Select an Enemy C#

Is it possible to use a RayCast (idk know what a raycast is) to select an enemy? Like if i have a variable lets say

public Transform EnemySelected;

And i wanted it if you clicked a object that had the script NPC_AI.cs that this variable would equal that enemy? If it is possible how could i go upon doing this?


Sorry i forgot to include this i have a tag also set up if its an enemy

For a good intro video to raycasting, click here

Also, the Unity documentation is your friend.

You can get the transform of an object that was hit with your raycast…

More information on raycasts can be found here