• 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 Sam Mbois · Jul 03, 2014 at 06:17 AM · c#stringintconvert

Convert string to int C#

I have some problem with my login. My login is so simple. User type name and then that name will be show up when it play. In my script I have some problem with convert string to int. I use playerprefs for saving that name, but playerprefs doesnt wont to saving the name. Please help me. I have to finished t$$anonymous$$s task in 1 week.

T$$anonymous$$s is my script

bool player1 = false; bool player2 = false;

 public static string EditPlayer1 = "";
 public static string EditPlayer2 = "";
 public static int IntEditPlayer1;
 public static int IntEditPlayer2;
 
 GUIStyle myFont;
 void start (){
     myFont = new GUIStyle ();
     myFont.fontSize= 12;
     
 }
 void OnGUI() {
     
     
     Event e = Event.current;
     
     if (e.keyCode == KeyCode.Return)
         player1 = true;
     else if (false == player1)     
         EditPlayer1 = GUI.TextField (new Rect (450, 230, 300, 50), EditPlayer1,200);
     IntEditPlayer1 = System.Int32.TryParse(EditPlayer1); <<< ERROR
     PlayerPrefs.SetInt ("Player1", IntEditPlayer1);
     
     if (e.keyCode == KeyCode.Return)
         player2 = true;
     else if (false == player2)   
         EditPlayer2 = GUI.TextField (new Rect (450, 310, 300, 50), EditPlayer2,200);
     IntEditPlayer2 = System.Int32.TryParse (EditPlayer2); <<< ERROR
     PlayerPrefs.SetInt ("Player2", IntEditPlayer2);
 }

 void Update(){
     Debug.Log ("EditUser"+EditPlayer1);
 }
Comment
Add comment · Show 1
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 HarshadK · Jul 03, 2014 at 06:19 AM 0
Share

What error do you get on the lines specified with '<<< ERROR' ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jul 03, 2014 at 06:32 AM

I use TryParse() when I don't need or want to deal with exception handling, but you still have to use it correctly...and you still need to figure out what to do if the conversion failed. It returns a boolean value indicating the success of the converstion. For your code it will be somet$$anonymous$$ng like t$$anonymous$$s:

 if (!System.Int32.TryParse (EditPlayer2, out IntEditPlayer2) {
    // Error handling goes here
 }

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 Sam Mbois · Jul 04, 2014 at 02:57 AM 0
Share

Thanks for your help. it helpful

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Converting a string to an int 2 Answers

Convert Text to float 3 Answers

Get JSON array object string value 2 Answers

Converting string of a key into KeyCode 2 Answers

GetHashCode() questions/Turning a text string into an int 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