Advertisement
UnityCoder_Jay

Sign.cs

Jan 8th, 2023 (edited)
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class Sign : MonoBehaviour
  5. {
  6.     public GameObject dialogBox;
  7.     public Text dialogText;
  8.     public string dialog;
  9.     public bool playerInRange;
  10.  
  11.     private void OnTriggerEnter2D(Collider2D other) {
  12.         if (other.CompareTag("Player")) {
  13.             playerInRange=true;
  14.         }
  15.     }
  16.  
  17.     private void OnTriggerExit2D(Collider2D other) {
  18.         if (other.CompareTag("Player")) {
  19.             playerInRange=false;
  20.             dialogBox.SetActive(false);
  21.         }        
  22.     }
  23.  
  24.     private void Update() {
  25.         if (Input.GetKeyDown("Interact") && playerInRange) {
  26.             if (dialogBox.activeInHierarchy) {
  27.                 dialogBox.SetActive(false);
  28.             } else {
  29.                 dialogBox.SetActive(true);
  30.                 dialogText.Text = dialog;
  31.             }
  32.         }  
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement