• 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
1
Question by robert 3 · Mar 09, 2011 at 11:50 AM · bce0044bce0018bce0022bce0051

character restriction

var text : string ;

function OnGUI () { GUI.skin.settings.cursorColor = Color.red; var chr : char = Event.current.character; if ( (chr < 'a' || chr > 'z') && (chr < 'A' || chr > 'Z') && (chr < '0' || chr > '9') ) { Event.current.character = '\0'; } text = GUI.TextField(new Rect(100,100,100,30), text,10); }

Assets/NewBehaviourScript.js(10,17): BCE0044: unexpected char: '''. error is coming can u rectify it

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

Answer by Statement · Mar 09, 2011 at 01:12 PM

I don't get that error mate, but I do get:

BCE0018: The name 'string' does not denote a valid type ('not found').

var text : string; // Should be String, not string.

Then I get further errors:

  • BCE0051: Operator '<' cannot be used with a left hand side of type 'char' and a right hand side of type 'String'.

  • BCE0051: Operator '>' cannot be used with a left hand side of type 'char' and a right hand side of type 'String'.

I replaced the if-test to:

if ( !char.IsLetterOrDigit(chr) )

BCE0022: Cannot convert 'String' to 'char'.

Event.current.character = '\0';

This one was a bit more tricky. It seems (I am not a JS coder really) that you can't set characters with '. I set it to 0 instead. I tried out the modified code and it seems to work. (Another way to set the character would been Event.current.character = "\0"[0];)

Here's the whole deal:

var text : String;

function OnGUI () { GUI.skin.settings.cursorColor = Color.red; var chr : char = Event.current.character; if ( !char.IsLetterOrDigit(chr) ) { Event.current.character = 0; } text = GUI.TextField(new Rect(100,100,100,30), text,10); }

Comment
Add comment · Show 3 · 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 robert 3 · Mar 09, 2011 at 02:35 PM 0
Share

can u please tell how above code work i am new to unity i tried hard to understand not getting any idea robert 0 secs ago

avatar image Statement · Mar 09, 2011 at 02:47 PM 0
Share

char.IsLetterOrDigit is a method that return true if the character is a letter, or if it is a digit. It might also support other characters such as **, so its logic is a bit different from the one you tried to write. So, we're using the ! operator which invert the result from char.IsLetterOrDigit, so the if test reads "if not a letter or digit". Event.current.character = 0 sets the character to value 0 (I am pretty sure it is same as \0). So, in effect, we're filtering out input when the typed character is anything else but a letter or digit.

avatar image robert 3 · Mar 09, 2011 at 02:58 PM 0
Share

thanks a lot it is working nicely

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Declaring char or string type variables returns errors 2 Answers

Error when accessing a script from another script (iOS) 1 Answer

Can't Start must fix all comiling errors. OxFFFD 2 Answers

BCE0044: expecting }, found 'class' alond with BCE0044: expecting EOF, found 'Card' 1 Answer

JavaScript: Accessing instantiated objects outside the instantiating function 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