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 /
  • Help Room /
avatar image
0
Question by wolfncrow · Apr 21, 2016 at 06:22 PM · c#stringloadlevelkeycodereturn

Premature level loading. String loads level, instead of Return key.

I'm trying to write a C# script, which works like PasswordField, but the users have to see what they are typing so I'm using TextField. The problem comes with submitting the string. First I had just the GUI.Button, which worked fine, but friend of mine said using Return key would be more intuitive. So now I'd like to have both.

When I type in the correct "password", the script loads the next level ("signal1") right away without me/the user hitting the Return key. (That's why the "signalWrong" is a comment line).

It's been a long time since I've been coding anything, so I have copied all of this KeyCode-stuff after trying to figure it out for couple of days. :-P I thought EventType.keyDown would solve the "self activating Return key", but no. Any help is greatly appreciated.

using UnityEngine; using System.Collections;

public class enterointi : MonoBehaviour {

 bool userHasHitReturn = false;
 string stringToEdit = "";

 void OnGUI()
 {

     GUI.skin.textField.fontSize = 30;
     GUI.Button(new Rect(Screen.width / 2 + 14, Screen.height / 2 + 80, 40, 40), "");

     Event e = Event.current;
     if (e.type == EventType.keyDown && e.keyCode == KeyCode.Return);
     if (e.keyCode == KeyCode.Return) userHasHitReturn = true;
     else if (false == userHasHitReturn) stringToEdit = GUI.TextField(new Rect(Screen.width / 2 - 140, Screen.height / 2 + 80, 150, 40), stringToEdit, 20);


     if (stringToEdit == "119")
     {
         Application.LoadLevel("signal1");
     }
     else if (stringToEdit == "admin123")
     {
         Application.LoadLevel("signal1");
     }
     else
     {
      //   Application.LoadLevel("signalWrong");
     }


 }
  

}

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 Scribe · Apr 21, 2016 at 06:45 PM

well, you don't have GUI.Button wrapped in a n if at all, so not sure how you plan to use that to move on, next you don't check useHasHitReturn before loading level. Finally, the lack of curly braces used for your if statements makes your code awfully difficult to read, which is pretty critical to getting people to help.

Here's a rewrite of your code elements, tidied up a little:

 void OnGUI(){
     GUI.skin.textField.fontSize = 30;
     userHasHitReturn = false;
 
     stringToEdit = GUI.TextField(
         new Rect(Screen.width / 2 - 140,
             Screen.height / 2 + 80,
             150,
             40),
             stringToEdit, 20);
 
     if(GUI.Button(new Rect(Screen.width / 2 + 14, Screen.height / 2 + 80, 40, 40), "")){
         userHasHitReturn = true;
     }
 
     Event e = Event.current;
     if(e.type == EventType.KeyDown && e.keyCode == KeyCode.Return){
         userHasHitReturn = true;
     }
 
     if(!userHasHitReturn) return;
 
     if(stringToEdit == "119"){
         Application.LoadLevel("signal1");
     }else if (stringToEdit == "admin123"){
         Application.LoadLevel("signal1");
     }else{
         Application.LoadLevel("signalWrong");
     }
 }

Final note, there is a new UI system implemented in unity now, using Event Systems and callbacks, granted it may be over kill for your use case. However it is worth noting, as OnGUI is rather inefficient (called twice per frame!) and is more or less deprecated except for editor functionality.

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 wolfncrow · Apr 22, 2016 at 12:44 PM

Thank you so much Scribe!

Curly braces... Of course. Didn't even realised them missing, because Unity didn't mention them. Really sorry about the messy code. My first post here.

Thank you again. I think next thing I'll do is update my Unity.

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 Scribe · Apr 22, 2016 at 01:46 PM 0
Share

No problem, you don't need curly braces, however it means only the 'line' directly afterwards is effected, see line 21 of my answer. Ending an if statement line with a semi-colon ';' will mean it doesn't fire at all, as with your line 11. In general always best to use curly braces, unless the function is obvious as with line 21 of my answer again. A few other notes, you should not use the new answer section to comment on things, rather, as I have done, use add comment at the bottom of what you wish to reply to.

Finally, if your question is answered, you can click 'Accept' at the bottom of the answer you wish to accept, that way others can find answers to similar questions more easily! (and I get karma :3). Have fun coding!

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

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

Related Questions

Convert keycode to string 1 Answer

Accessing a non-static string variable in another script C# 1 Answer

Accesing specific int from inspector 2 Answers

How do I save separate keycodes presses at same time and store for use later in update? 0 Answers

I'd like my object to be active only when space key is NOT pressed 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