How can i show to the screen an object name when my raycast hit him?

So i created a raycast that “shoots” from the center of the camera and i want to know to do make a little text (object’s name) appear on the screen when i hits an object.
So here i started the code but i don’t know how to make it:

static RaycastHit hit;
Vector3 forward = transform.TransformDirection (Vector3.forward) * 50;
Debug.DrawRay (transform.position, forward, Color.black);
        if (Physics.Raycast (transform.position, forward, out hit)) {
}

I will appreciate any support . Thanks!

There are two parts to this question, it seems:

  1. How do you get the name of the object?
  2. How do you display it?

The first part is simple: hit.gameObject.name

The second part depends on the answers to other questions. For example, do you want this text to be in screen space (e.g. part of the UI, just hovering in position of the hit) or in world space (actually rendering as a part of the world)? Also, is this functionality something you expect to use often, because if so, it may be worth generalizing it.

1 Like

If it is for debugging, make a Unity UI Text field, drag a reference to it into your screen, and put the information you want in the .text property at the right time.

If it is “for reals,” look into putting Unity UI Text objects into the scene (requires some canvas setup and parenting to make it stay close to where you want) and then do the same setting of the .text property.

ok i will try thanks :smile:

i just want to be the text on the screen, to appears when the raycast hit the object

Yeah i finnaly figuered out thank you very much

1 Like

Hi! I have the same problem how you did? Thanks for help!