• 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 Markive · Feb 25, 2014 at 01:10 AM · guitext

Allowing user to edit GUIText - problems

Sorry for the newb question, I've been playing with this for an hour or two and encountering very confusing behavior. I have tried to research this question as well but not found my answer.

I have a GUIText object that states "Player Name: Player" and I wish for this to be editable by the user.

The below script will delete characters from my GUI label's text on pressing backspace key, however will not allow the user to add any character.

If I un-comment out and insert the debug.log line, things start to get 'freaky', it will append as many characters as you type to the end of my GUIText.text obejct, but only ever permits a single character to be deleted on backspace, until you type a further character and then it allows another single character to be removed..

Why does the behavior change when I add in the log line?!

This has me thinking that perhaps c is not defined/declared correctly and can not be added correctly to the GUIText.text property due to this? I have tried setting c to a private, public and static char to no avail.

Clearly I am missing something fundamental here, any pointers?

         foreach (char c in Input.inputString) {
             if (c == "\b"[0])
                 if (guiText.text.Length != 0)
                     guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);
             
             else
                 if (c == "\n"[0] || c == "\r"[0])
                     print("User entered his name: " + guiText.text);
             else
                 guiText.text += c;
 //            Debug.Log (guiText.text +=c);
         }
 
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
2
Best Answer

Answer by robertbu · Feb 25, 2014 at 01:39 AM

Your 'else' is not binding to the 'if' that you think it is. When all 'else' fails, add brackets (pun intended). Try this:

 foreach (char c in Input.inputString) {
     if (c == '\b') {
         if (guiText.text.Length != 0) {
             guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);
         }
     }
     else {
         if (c == "\n"[0] || c == "\r"[0]) {
             print("User entered his name: " + guiText.text);
         }
         else {
             guiText.text += c;
         }
     }
 }
Comment
Add comment · Show 3 · 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 Markive · Feb 25, 2014 at 01:50 AM 0
Share

Rah! Thank you robertbu for the quick response, awesome!

I do normally use the brackets myself, but copy/pasted the above script from the unity reference (ctrl-' lookup) and it never occurred to me!

Out of interest, I note you've slightly changed the backspace char on line3 "b"[0] to 'b'

What does the [0] do in this instance and any difference between single and double quotation marks?

avatar image robertbu · Feb 25, 2014 at 01:54 AM 0
Share

Double quotes is a string. Single quotes is a character. So the original code created a string with a backspace character, then indexed into the string to grab the character. I just simplified it a bit by creating a character constant.

I'm guessing, but I'll bet the script used this method because it is what you have to do in Javascript. The Javascript version of the example script was probably created first, and then someone just did a literal translation into C#.

avatar image Alexphauge · Jun 04, 2014 at 10:27 PM 0
Share

Thanks so much, really had problems trying to delete a character by using Input.Get$$anonymous$$eyDown and getting backspace from that. Apparently then when using Input.inputString it has some hidden characters adding length to the string all the time. So yeah, thanks a lot for this one! :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How would I make the text bigger on this script? 1 Answer

Displaying my variable on a gui text 0 Answers

Detecting GuiText click 0 Answers

Problem Displaying Score on GUI text 0 Answers

GuiStyle attachment problem 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