Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bardon · Apr 15 at 02:57 AM · 2dtexttop downsign

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bardon · Apr 16 at 01:25 AM 0
Share

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.

avatar image valarken Bardon · Apr 17 at 08:03 AM 0
Share

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

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

313 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My text is invisible if I put it under the Content object under a Scroll View 1 Answer

Using a trigger to make a scrollview appear and then disappear? 1 Answer

Why wont my text box stay the same size? 2 Answers

How to display UI Text on GameObject in script? 0 Answers

Carry over texts from one scene to another? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges