Getting the position of a specific gameobject using tags

Hi,

the plan I have in mind is as follows: I am sending a raycast into my scene. There are multiple identical gameobjects in the scene at different positions but all have the same tag (e.g. “A”). If the raycast hit a gameobject with the tag “A” a few different things are supposed to happen. One of them is getting the position of the very gameobject the raycast hit but in the best case without getting any more specific then the tag “A”. So I would rather not correspond to this gameobject using its name but rather keep my code variable to fit every Object with the tag “A”. After that I am doing something else with the position which I already have the way I want it.

So in conclusion my question is: Is there a way to get the position of a specific gameObject which a raycast did hit, with just using the tag feature?

If anything is unclear or you need further information please let me know.

Thanks in advance

Raycast actually gives you the list of gameObjects it hit. Not directly, it returns RaycastHit object (or array, depends on how you are raycasting) that has the information about the transform that was hit. You can then compare the tags

RaycastHit.transform.gameObject.Tag == “A”;

and get it’s position with

RaycastHit.transform.position;

Yes there is! On your raycast hit code you do this:

GameObject.FindWithTag("A").transform.position;

Here are the docs for that function.

Great! Thanks guys! I will have a look if I can manage to get the result I want. :slight_smile: