Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 dancop25 · Jan 30, 2017 at 03:37 PM · c#instantiatetextruntime

Instantiate and edit a text box during runtime

I'm trying to make it so that users can create a text box in worldspace when they start typing on the keyboard.

So far I have a prefab containing a Text(Script) component which has all the desired text box preferences. I'm having trouble figuring out how exactly the player can access the text field in this box and edit it during gameplay. So far the closest thing I've gotten is having a separate TextArea in the GUI and then passing that string to the text in the Text(Script) component, but surely there's a better way.

Here's a screenshot to help illustrate what I've gotten so far. The TextArea is the entry field on the top left of the screen and the floating text in the worldspace is the text box whose text I want to edit during runtime.

alt text

screen-shot-2017-01-30-at-125425-copy.jpg (357.0 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Loui_Studios · Jan 30, 2017 at 03:56 PM

Use GetComponent() to get the Text component on the prefab.

By modifying Text.text, the text of the component will change.

A simple script would look something like this:

 public GameObject textPrefab;
 Text textPrefabText;

 void Start() {
 textPrefabText = textPrefab.GetComponent<Text>();
 
 textPrefabText.text = "Woohoo! I'm writing text at runtime!";
 }

But if you want to modify it in one line of code, you would do this:

 void Start() {
     textPrefab.GetComponent<Text>().text = "Woohoo! I'm writing text at runtime!";
 }
Comment
Add comment · Show 4 · 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 dancop25 · Jan 30, 2017 at 05:06 PM 0
Share

Thank you for your answer. I understand how I can edit the text in the script but how would the player do so dynamically during gameplay?

avatar image Loui_Studios dancop25 · Jan 30, 2017 at 05:50 PM 0
Share

Well, you're on the right track with the text field. I assume you want the player to enter the text, and have the text update on screen immediately.

So if you have a text input in your scene already, you should notice when you scroll down, a list box called "OnValueChanged".

If you click the '+' symbol, you can attach a script to this, and have the text field call a public function from that script whenever the text input changes.

Write a function that takes the text from the input field, and sets the text on screen to that value. Then attach the script and function to the OnValueChanged list.

When you type into it in play mode, the text on screen will update automatically.

avatar image ia559918 · Mar 30, 2021 at 01:07 PM 0
Share

Thank You it helped me update coins text on runtime after watching the ad.

avatar image Loui_Studios ia559918 · Mar 31, 2021 at 08:52 AM 1
Share

No problem :)

Btw since writing this post I learned that GetComponent() is not a very efficient way of getting a reference as it generates surprising amounts of garbage. I would recommend only using GetComponent() once, caching the value, and then reading from the cached value to save calling that command more than neccesary. Properties are a pretty clean way of doing this. Another approach would also be dependency injection, if you prefer that route.

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

11 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

Related Questions

How to access value within instantiated class in different object? 1 Answer

Change The GuiText.text by code 1 Answer

How do you Instantiate Text as Child of Canvas c# 1 Answer

How to instantiate prefab over the text one letter at a time? OR how to get transform of letters in a text 0 Answers

Distribute terrain in zones 3 Answers


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