• 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
Question by hollym16 · Sep 10, 2013 at 03:24 PM · stringonguitextfieldelse

TextField Query

Hi there,

I've got the following script

 #pragma strict
 var stringToEdit : String = "Type Code Here";
 
 function OnGUI(){
 
     stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25);;
     
     if (GUI.Button (Rect(Screen.width/2,Screen.height/2,80,20), "Submit")){
     
     if(stringToEdit == "LCD TV"){
         Application.LoadLevel ("TVscene");
     }
     if(stringToEdit == "Camera"){
         Application.LoadLevel ("Camerascene");
     }
         
         }
         }

Which works great. However, if the user types in something in the textfield that isn't specified in the code, I want some sort of error message to come up (i.e. "Error: Invalid Keyword"). Could I achieve this with an else statement perhaps?

Anyone have any ideas?

Thanks

Comment

People who like this

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

Answer by AlfredDuler · Sep 10, 2013 at 03:36 PM

 var StringIfError : String = "";

 function OnGUI()
 {
 
     stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25);

     StringIfError = "";

    if(stringToEdit != "LCD TV" && stringToEdit != "Camera")
    {
       StringIfError = "Error: Invalid Keyword";
    }

     if (GUI.Button (Rect(Screen.width/2,Screen.height/2,80,20), "Submit"))
     {
         if(stringToEdit == "LCD TV")
         {
            Application.LoadLevel ("TVscene");
 
         }

         if(stringToEdit == "Camera")
         {
            Application.LoadLevel ("Camerascene");
  
         }

         
    }

    GUI.Label(Rect(10, 30, 100, 20), StringIfError );

 }
Comment

People who like this

0 Show 9 · 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 AlfredDuler · Sep 10, 2013 at 03:47 PM 0
Share

See below

avatar image hollym16 · Sep 10, 2013 at 03:51 PM 0
Share

Yeah, that doesn't seem to work, it does nothing. It's meant to be that when I type anything other than "LCD TV" or "Camera" it comes up with the error message

avatar image AlfredDuler · Sep 10, 2013 at 03:51 PM 0
Share

The OnGUI function is an update function. In my first answer, the Label with the error was only displayed the frame you clicked the button, so something like 1/200 seconds = Invisible;

So in the new answer: you set a label to display an empty string, so it isn't visible, until the keword don't match then it's set to "Error..."

avatar image AlfredDuler · Sep 10, 2013 at 03:56 PM 0
Share

Yeah sorry not woke up.... That should be good now

avatar image hollym16 · Sep 11, 2013 at 09:00 AM 0
Share

Great, it works! Thank you for the help. Unfortunately I can't accept your answer as correct because its a comment!

Show more comments

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

how to check GUI.TextField Entry 2 Answers

How to make GUI disappear forever? 2 Answers

Input a string and the computer answers with a command? 4 Answers

converting "int" to "string" 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