- Home /
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.Get$$anonymous$$eyDown 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.
Answer by valarken · Apr 15 at 01:51 PM
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 :)
Notes : You can still walk when viewing the UI Box and text, if u want to disable the player movement, u need another script.
Ok so im just a little confused, so after you add the script to your player what do you do? I was reading the comments on the script and saw signbox and signtext were for those ui box and ui textbox, so I created an empty game object on my canvas for my ui, and added my image component for the box that i'd like to use and then anchored it to the top. After that I created an empty game object, then added the text component, changed the font and font size, but left the text empty, because I thought that the next part in the script, the sign, it says your UI text. That's all I've done so far but I don't know if im doing it right or what to do after that.
Sorry, i made a mistake. U need to sign the script to the object. You can learn more here : https://www.youtube.com/watch?v=1NCvpZDtTMI&list=PL4vbr3u7UKWp0iM1WIfRjCDTI03u43Zfu∈dex=12
Answer by maross334 · Apr 16 at 03:30 AM
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.