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 drex150 · Aug 01, 2014 at 06:07 PM · systemchat

Chat script works in editor but not in stand alone.

Hey all,

So I have a bit of an interesting problem. I've not run in to anything like this before so hopefully someone will figure out why this is happening.

Anyway, I am trying to set up a chat window. When you hit enter/return, it will open the chat, you can then type your message and press enter/return again to send the message. The code I have below works in the Unity editor. As soon as I make an .exe version, the script opens the chat, then quickly closes the chat before I can type anything. If I'm fast I can type a letter before it closes. If I type a letter fast enough, the window stays open and functions as expected.

Here is my code:

 if(chatOpen)
             {
                 GUILayout.BeginArea( new Rect(10 ,Camera.main.pixelHeight - 125, 300, 50));
                 GUILayout.BeginHorizontal();
                 GUILayout.BeginVertical(GUILayout.Width(300));
                 GUI.SetNextControlName("myMessage");
                 myMessage = GUILayout.TextField(myMessage);
                 if(textControl)
                 {
                     print("textControl triggered");
                     GUI.FocusControl("myMessage");
                     textControl = false;
                 }
                 GUILayout.EndVertical();
                 GUILayout.EndHorizontal();
                 GUILayout.EndArea();
                 if(Event.current.keyCode == KeyCode.Return && chatSend > .1f)
                 {
                     print("keyCode Event Triggered");
                     GUI.SetNextControlName("");
                     GUI.FocusControl("");
                     sendNow = true;
                 }
             }

The chatSend float was how I got it to work in the editor. Basically, you press enter, then it waits 0.1 seconds before you can press enter again to send the message. This prevents the chat opening and then closing right after it opens because OnGUI runs more than once per frame. I am receiving no errors when I run this and if I increase the time for chatSend from 0.1f to something like 0.3f it will wait 0.3 seconds and then close the chat window.

If you have any ideas why this is happening I'd really appreciate it.

Thanks!

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
0
Best Answer

Answer by drex150 · Aug 03, 2014 at 06:41 PM

I found an answer to this after some testing. Here is my code.

 if(chatOpen)
         {
             if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return)
             {
                 print("keyCode Event Triggered");
                 GUI.SetNextControlName("");
                 GUI.FocusControl("");
                 sendNow = true;
             }
             if(Camera.main != null)
             {
             GUILayout.BeginArea( new Rect(10 ,Camera.main.pixelHeight - 125, 300, 50));
             GUILayout.BeginHorizontal();
             GUILayout.BeginVertical(GUILayout.Width(300));
             GUI.SetNextControlName("myMessage");
             myMessage = GUILayout.TextField(myMessage);
             if(textControl)
             {
                 print("textControl triggered");
                 GUI.FocusControl("myMessage");
                 textControl = false;
             }
             GUILayout.EndVertical();
             GUILayout.EndHorizontal();
             GUILayout.EndArea();
             }
         }
 

I moved over event.current.keycode and added event.current.type = eventtype.keydown. I also moved this all above the text box's creation. This worked perfectly as such.

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
avatar image
0

Answer by shock2provide · Aug 01, 2014 at 08:16 PM

Try Input.GetKeyDown(KeyCode.Return) instead of Event.current.keyCode == KeyCode.Return.

This checks the KeyPress only once per frame.

Regards

Marc

Comment
Add comment · Show 1 · 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 drex150 · Aug 01, 2014 at 09:24 PM 0
Share

Because I'm changing the FocusControl to the text box, using Get$$anonymous$$eyDown will not do anything. The only way I've found to work is what I have currently. Any other method for checking if a button is pressed simply doesn't do anything.

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

23 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

Related Questions

Type UnityEngine does not contain a definition for RPC 1 Answer

How to have both, a scroll view AND vertical Layout? 1 Answer

Accessing local system ( File Browser ) 2 Answers

C# help in walking and jumping 1 Answer

Creating a Quest system 2 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