using UnityEngine; using UnityEngine.UI; public class Proximity : MonoBehaviour { public string newTitle; public string newDesc; private Transform other; private Text myTitle; private Text myDesc; private float dist; public float minDistance = 4.0f; public float maxDistance = 5.0f; private GameObject player; private GameObject message1; private GameObject message2; private bool check; // Start is called before the first frame update void Start() { player = GameObject.FindWithTag("Player"); other = player.GetComponent(); message1 = GameObject.FindWithTag("ArtTitle"); message2 = GameObject.FindWithTag("Description"); myTitle = message1.GetComponent(); myTitle.text = ""; myDesc = message2.GetComponent(); myDesc.text = ""; check = false; } // Update is called once per frame void Update() { if (other) { dist = Vector3.Distance(transform.position, other.position); print("Distance to player: " + dist); if (dist < minDistance) { myTitle.text = newTitle; myDesc.text = newDesc; check = true; } if (dist > maxDistance && check) { resetText(); } } } private void resetText() { myTitle.text = ""; myDesc.text = ""; check = false; } }