How can I add UI Text and UI Box to show up when pressing a button near an object?

I found a similar question asked in Unity Answers and the answer was

“quick answer : - use triggers to check if you’re in front your sign or not - create ui elements for your text - add a script on your trigger with the OnTriggerEnter method, and activates a boolean variable for the next step - in an update on a script in the scene, add a if conditioner to check the input used (Input.GethieyDown or Input.GetButtonDown) - assign your ui elements in your script - activate or desativate your ui elements with the gameObject.SetActive method”

However, I don’t know what this looks like on a script, and the original poster also asked for an example, but their was no follow up, I’d like to ask what this would look like in a script, because I do not know how to code.

Hi, i will try to help.

First, u need to assign your player with tag “Player”

Then assign this script to your object. (Script name : SignAndPoster)

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

public class SignAndPoster : MonoBehaviour
{

    public GameObject signBox; //your UI Box
    public Text signText; //your UI TextBox
    public string sign; //your UI Text
    public bool playerInRange; //checking if player in range


    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E) & playerInRange) //Show the UI Box and Text when pressing "E" on keyboard
        {
            if (signBox.activeInHierarchy)
            {
                signBox.SetActive(false);
            }
            else
            {
                signBox.SetActive(true);
                signText.text = sign;
            }
        }

        if (Input.GetKeyDown(KeyCode.Space) & playerInRange) //Hide the UI Box and Text when pressing Space on keyboard
        {
            if (signBox.activeInHierarchy)
            {
                signBox.SetActive(false);
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D other) //Check if player enters the trigger
    {
        if (other.CompareTag("Player") && !other.isTrigger)
        {
            playerInRange = true;
        }
    }

    private void OnTriggerExit2D(Collider2D other) //Check if player exits the trigger
    {
        if (other.CompareTag("Player") && !other.isTrigger)
        {
            playerInRange = false;
            signBox.SetActive(false);
        }
    }
}

Tell me if you need more help :slight_smile:

Notes : You can still walk when viewing the UI Box and text, if u want to disable the player movement, u need another script.

Have the UI object set inactive in your hierarchy (just the parent should be inactive), and have a reference to that parent in your script. Add a collider on the object that you need to be close to. Make it as big as desired, and make sure to set it as a trigger.

bool canPullUpUI;
GameObject uiBox; // your ui box you want to pull up (parent game object reference)

private void OnTriggerEnter(Collider other){

  if(other.GetComponent<CustomButtonScript>() != null) // check if we entered the area
    canPullUpUI = true;

}

private void OnTriggerExit(Collider other){

  if(other.GetComponent<CustomButtonScript>() != null) // check if we left the area
    canPullUpUI = false;

}

if(canPullUpUI && Input.GetButtonDown("a")) // make sure we can pull up the UI/pressed button
  uiBox.SetActive(true);

This is assuming you have a script on this object named ‘CustomButtonScript’. you could use CompareTag, and compare if collisions gameobject’s name is the same as your ui box. Getting the component is just a quick easy way to check collisions.