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 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
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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
Add comment · 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.

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

Why do I have to call ToString() when fetching a String from another script? 2 Answers

Decreasing Length of a String? 2 Answers

How to tell if a string contains some specified text 6 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