Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 jeffreywarf · May 20, 2013 at 05:47 AM · guidisabletextfieldhide

Disabling/Hiding a Text Field with Enter Key

Here's what I have so far...

 var naming1 : boolean = false;
 var slot1_namer : GUI;
 var slot1_name : String;
 
 function Update (){
 
      if(Input.GetKeyDown(Keycode.Return))
      {
          if(naming1 == true)
          {
          naming1 = false;
          }
          else if(naming1 == false)
          {
          naming1 = true;
          }
      }
 }
 function OnGUI (){
      if(naming1 == true)
      {
      slot1_namer.enabled = true;
      slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
          if(Input.GetKeyDown(Keycode.Return))
          {
          slot1_namer.enabled = false;
               slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
          }
      }
      else if(naming1 == false)
      {
       slot1_namer.enabled = false;
               slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
      }
 }

redundant in the end, I know, but I was REALLY trying to get this to work. So how can I disable (or ideally "hide") the textfield when "return" or "enter" is pressed?

EDIT: New development, every time I leave then go back to the menu with the TextFields, the ability for "enter" to enable and disable them is reset. Meaning that as long as none of the fields are edited, the code works properly and they fields enable and disable. yet if any of them are edited, this stops working so I have to back out then go back in.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Tomer-Barkan · May 20, 2013 at 06:13 AM

How about:

 public class SomeClass : MonoBehaviour {
     private bool textFieldEnabled = true;
     private string textFieldText = "";
 
     void update() {
         if(Input.GetKeyDown(Keycode.Return)) {
             textFieldEnabled = !textFieldEnabled;
         }
     }
 
     void OnGUI() {
         if (textFieldEnabled) {
             textFieldText = GUI.TextField(new Rect(0, 0, 100, 25), textFieldText);
         }
     }
 }
Comment
Add comment · Show 7 · 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 jeffreywarf · May 20, 2013 at 06:23 AM 0
Share

didn't work. I mean, it initially hides it but afterwards I still run into the same problem of "return" not disabling it.

avatar image Tomer-Barkan · May 20, 2013 at 06:40 AM 0
Share

my bad, you shouldn't handle key inputs in OnGUI(). See http://answers.unity3d.com/questions/243995/getkeydown-problem.html for more info. Updated the code so that it now works (tested myself)

avatar image jeffreywarf · May 20, 2013 at 06:54 AM 0
Share

Still not working on my end. I press enter, the TextField stays. But like before it hides it initially then shows it after clicking on the button that shows it. This is a lot more complicated than the posted code, but I can't post it all since it's over 300 lines long. Suffice to say, there is a button that, when clicked sets na$$anonymous$$g1 to true. then when I press enter, it SHOULD set it to false, thereby disabling or hiding the textfield. Yet this is not the case.

avatar image Tomer-Barkan · May 20, 2013 at 06:57 AM 0
Share

$$anonymous$$y code works perfectly, I tested it. So if I'm to help you I'll need more info on whats different between your code and $$anonymous$$e.

avatar image jeffreywarf · May 20, 2013 at 07:03 AM 0
Share

well... is there a place I can upload the full code? it's well over the 3000 character limit.

I'm sure nothing is wrong with your code, $$anonymous$$e is just so tangled anything could be going wrong

Show more comments
avatar image
0

Answer by LukaKotar · May 20, 2013 at 06:33 AM

Try removing all lines from the 24th to 28th line. Basically in the Update() you are changing the boolean to the opposite value, so if you set it to 'false' in OnGUI(), the Update() will turn it 'true'.

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 jeffreywarf · May 20, 2013 at 06:44 AM 0
Share

I tried when it was just in the update function and when it wasn't, both times I'm still not getting exactly the desired result. It seems as though using GUI buttons will disable/hide the buttons (this is only a portion of a much larger script utilizing many buttons) because when I click the "back" button and access the menu again (through a different button) then they're all disabled and uneditable.

I should be more specific...

They're all part of a menu that has "resume, save, load, and exit" on it. When going to "save" the text fields aren't having their desired result. But when I press "back" from the saving/loading GUI group then go back to the saving/loading GUI group via the "loading" button (the only difference is a variable called "loading" that is a boolean that is set to true while "saving" is set to false, it also disables na$$anonymous$$g1 but seems to actually have an effect) then it works.

avatar image LukaKotar · May 20, 2013 at 06:58 AM 0
Share

Actually there was a mistake on my side here - I thought you were setting 'na$$anonymous$$g1' to false here... $$anonymous$$aybe try making the Update() function handle all the input. That way you can have more control over when the events happen. Try doing something like this:

 var na$$anonymous$$g1 : boolean = false;
 var slot1_namer : GUI;
 var slot1_name : String;
  
 function Update (){
  
      if(Input.Get$$anonymous$$eyDown($$anonymous$$eycode.Return))
      {
          if(na$$anonymous$$g1 == true)
          {
          slot1_namer.enabled = false;
          slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
          na$$anonymous$$g1 = false;
          }
          else if(na$$anonymous$$g1 == false)
          {
          na$$anonymous$$g1 = true;
          }
      }
 }
 function OnGUI (){
      if(na$$anonymous$$g1 == true)
      {
      slot1_namer.enabled = true;
      slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
      }
      else if(na$$anonymous$$g1 == false)
      {
       slot1_namer.enabled = false;
               slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
      }
 }
avatar image
0

Answer by unimechanic · Jul 03, 2013 at 04:08 PM

This example might also help:

http://forum.unity3d.com/threads/188202-Simple-dynamic-list-editor

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 Franco-Marini · Aug 19, 2021 at 01:31 AM

Use this code. It will solve all focus problem and key detection. @jeffreywarf

 public class MyConsole : MonoBehaviour
 {
     // Start is called before the first frame update
     public bool showConsole = false;
     public string guiText = "";
     void Start()
     {
         
     }
 
     void Update()
     {
         if (Input.GetKeyUp(KeyCode.F5)) showConsole = !showConsole;
         
     }
     private void OnGUI()
     {
         if (showConsole) {
             GUI.SetNextControlName("MyTextField");
             GUI.FocusControl("MyTextField");
             guiText = GUI.TextField(new Rect(10, 10, 1000, 20), guiText, 200); 
         }
         if (Event.current.keyCode == KeyCode.Return && showConsole)
         {
             //DO SOMETHING WITH THE TEXT AND THEN CLEAR TEXT
             //ADD SOME CODE
             guiText = "";
         }
     }
 }
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

17 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

Related Questions

Hide GUI while switchcam 3 Answers

Backspace in limited length textfield causes ArgumentOutOfRangeException 1 Answer

GUILayout Textfield not editable just static 1 Answer

Android Textfield locks out input until esc 2 Answers

Text.Field erratically updating 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