How do I get the Transform of the object im looking at in script?

I want to get a script for a physics based shooter. and I’ve got most of it sorted but i currently am stuck on a part of code where i want to be able to left click on an object i am looking at in the center of screen to be “selected”
It would come to the center of screen in front of me.
I already have the code for it to move towards the screen but I’m not sure how i would be able to select any object with rigid body to become referenced in the script

If that isn’t easily understandable.
I want a public variable that when i click on an object its transform will become saved into a script in a transform variable.

If i could get a solution in Java that’d be good but i can deal with c#

So, what you’d probably want to do is shoot out a raycast, since the engine doesn’t just know what’s in the center of your screen. Raycasting allow us to do that. You can get the information of whatever it is the raycast hits, including scripts and subsequent public variables.

Unfortunately, I’m not familiar with Java, so I can’t give you an example in Java, but this is roughly what it would look like in C#

RaycastHit hit;
if(Physics.Raycast(Vector3.zero, Vecor3.forward, out hit))
{
     GameObject hitObject = hit.collider.gameObject;
}