Use GameObject as a variable?

I want to use raycast to get the object that it’s hitting’s name. I have a variable called “targetItem” (public GameObject targetItem;) but it causes errors. Is there a way to use GameObject as a variable?

You can use a raycast to populate a “RaycastHit” structure. The RaycastHit can tell you which collider you hit. The collider can tell you which GameObject it belongs to.

See here for an example.