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 Eqric · Jan 08, 2013 at 02:24 PM · guistringchat

Strange string appending

Hi, i am trying to make a basic chat window, but when i try to build the string from the character pressed by the user on the keyboard, it get very strange. If i write the following message "Hello", the actual string seems to look something like this: "H e l l o" and not "Hello" as i want it to be.

I can see the error when i try to delete a character in the inputTextBox window. I need to press backspace twice for one character to be removed.

 public class ChattWindow : MonoBehaviour {
     
     private string inputField = " ";
     private Vector2 scrollPosition = Vector2.zero;
     //StringBuilder sb;
     private string ChattWindowText = "";
     // Use this for initialization
     void Start () {
         //sb = new StringBuilder();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     void Awake(){
         
     }
     void OnGUI(){
         Event e = Event.current;
         int i = 0;
         if(e.type == EventType.keyDown && e.keyCode == KeyCode.Return){
             ChattWindowText = ChattWindowText + inputField;
             
             while(i != inputField.Length){
                 Debug.Log(inputField[i]);
                 i++;
             }
             
             inputField = "";
             
             
         }else if(e.type == EventType.keyDown && e.keyCode == KeyCode.Backspace){
             inputField = inputField.Substring(0,inputField.Length - 1);
             Debug.Log(inputField.Length);
             Debug.Log(inputField);
             
         }else if(e.type == EventType.keyDown){
             inputField = inputField + e.character;
             
         }
         
         GUILayout.BeginArea(new Rect (0,Screen.height - 250,300,200));
         scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(300), GUILayout.Height(200));
         
         GUILayout.Label(ChattWindowText);
         GUILayout.EndScrollView();
         
     
         GUILayout.EndArea();
         GUI.TextField (new Rect (0,Screen.height - 50,300,50), inputField);
     }
     
 }
Comment
Add comment · Show 4
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 Graham-Dunnett ♦♦ · Jan 08, 2013 at 03:12 PM 1
Share

OnGUI can be called multiple times. How many times does your else if(e.type == EventType.keyDown){ code get executed per frame?

avatar image Eqric · Jan 08, 2013 at 03:16 PM 0
Share

Oh, it seems it get called two times, why does OnGUI get two calls?

avatar image Graham-Dunnett ♦♦ · Jan 08, 2013 at 03:58 PM 0
Share

Because that's the documented behaviour, and because you might get multiple events.

avatar image Landern · Jan 08, 2013 at 04:21 PM 2
Share

$$anonymous$$ove your text update logic(key presses and such) to the update function/method, your inputfield is accessible from out side the functions/methods.

0 Replies

· Add your reply
  • Sort: 

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

10 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

Related Questions

Textfield doesnt work; it doesnt apply string 0 Answers

format text block in GUI.Box 1 Answer

changing GUI Button text with a string array 2 Answers

How can I Include string variables in Rich Text? 1 Answer

Find what word is selected by mouse in GUI.Label 0 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