In Unity 5.4.1 I'm trying to add a tag that says 'press E to open' to a door as you approach it. I've got a RayCast pointing forward from the player, detecting the door and , connected to my PlayerController, I've got a copy of a prefab GameObject which itself contains a Text UI element. The GameObject _interactionTag is instantiated into the scene when the player is close enough.
But I'm having some trouble getting it to display correctly in the scene and I'm not sure why.

Here are the relevant parts of my PlayerController:

public class PlayerController : MonoBehaviour {
    public Canvas _canvas;
    private Vector3 _fwd;
    private RaycastHit _hit;
    public GameObject interactiveTag;
    private GameObject _interactiveGameObject = null;
    private GameObject _interactionTag;

    void Update () {
        _fwd = transform.TransformDirection(Vector3.forward);
        if (Physics.Raycast(transform.position, _fwd, out _hit, 3)) {
        if (_hit.distance<1) {
            _interactiveGameObject = _hit.collider.gameObject;
            GameObject _interactionTag = (GameObject) Instantiate(interactiveTag, _interactiveGameObject.transform.position, _interactiveGameObject.transform.rotation);
           _interactionTag.GetComponentInChildren<Text>().text = _interactiveGameObject.GetComponent<IsInteractive>().interactionText; // this gets a text string from the door 'press E to open'
           _interactionTag.transform.SetParent(_canvas.transform, false);

In game, the _interactionTag is appearing - though it's backwards and does not appear attached to the door. Also is any of this the best way of getting the 'press E to open' tag to appear attached to the center of the door? I'm beginning to wonder if what I'm doing is best practise. Thanks for any help.


1 Answer 1


I think in this case it would be much easier if you just add the world-space canvas to the prefab as a child and disable it at start-up. Then in your code you just enable it when your distance condition is true and vice versa.

  • \$\begingroup\$ Thanks for your reply. I think I was slightly in two minds about how I wanted it to look, actually. I've ended up using a canvas set to 'overlay' and using Camera.main.WorldToScreenPoint(position) to convert the target object's transform.position to screen coordinates. Seems to work well. \$\endgroup\$ Nov 9, 2016 at 16:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .