Interaction Text Box

I am trying to make a text box appear when I get close enough and left click. I can make the box appear, but I can’t figure out how to make it disappear. I know that it is probably something very simple, but i’m running low on time for my game. Does anyone know how to disable my text box?

The bottom part of my script is the part i’m having trouble with.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Camera_Raycast : MonoBehaviour {

private Camera _camera;

public RectTransform _rightLine;
public RectTransform _leftLine;
public RectTransform _topLine;
public RectTransform _bottomLine;

public Image chairBackground;
public Text chairText;

private bool chairInfoIsEnabled;

void Start() {
	_camera = GetComponent<Camera> ();
	chairBackground.enabled = false;
	chairText.enabled = false;
}
void Update () {
	Vector3 point = new Vector3 (_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
	Ray ray = _camera.ScreenPointToRay (point);
	RaycastHit hit;	

	chairInfoIsEnabled = false;

	if (Physics.Raycast (ray, out hit, 5)) {
		if (hit.collider.tag == "InspectableObject") {
			_rightLine.localPosition = new Vector3 (5, 0, 0);
			_leftLine.localPosition = new Vector3 (-5, 0, 0);
			_topLine.localPosition = new Vector3 (0, 5, 0);
			_bottomLine.localPosition = new Vector3 (0, -5, 0);

		} else {
			_rightLine.localPosition = new Vector3 (10, 0, 0);
			_leftLine.localPosition = new Vector3 (-10, 0, 0);
			_topLine.localPosition = new Vector3 (0, 10, 0);
			_bottomLine.localPosition = new Vector3 (0, -10, 0);
		}
			

		if (hit.collider.tag == "InspectableObject" && hit.collider.name == "TestObject") {
		
			if (Input.GetMouseButtonDown (0) && chairInfoIsEnabled == false) {
				Debug.Log ("Activate Info");
				chairBackground.enabled = true;
				chairText.enabled = true;
				chairInfoIsEnabled = true;

			}
		

		}
	}
}

}

If you need any info, I’ll respond.

Good Day.

You can check the distance between the player and the object with

Vector3.Distance()

And execute something if is higger than a value in a if sentence.

By disapear you mean to stop showing the text? You can go to text of the Text Component and use the enabled=false

Or you can deactivate the whole text gameobject by SetActive(false)

Or even destroy the textGameObject by Destroy(TextGameObject)

If this is not what you was asking, please explain more.

Bye! :smiley: