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
1
Question by ObviouslyInsane · Apr 28, 2012 at 04:09 AM · stringintconvertparse

Convert int to string and back again

Hi all, I'm having a problem after I convert one of my int's to a string..I can't get it to convert back to an int! Here's some sample code to show you what i'm dealing with.

 var price : int;
 
 function IntToString() {
 price.ToString();
 }
 
 function StringToInt() {
 price = System.Int32.TryParse(price); <<<ERROR HERE
 }

I'm getting the error "No appropriate version of int.TryParse for the argument list '(int)' was found."

Can someone give me an idea what I'm doing wrong? Thank you!

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

Answer by Fabkins · Apr 28, 2012 at 11:37 AM

Here you go:

  var nu1: int = 100;
  var str: String ="";
  var nu2: int;

  // Convert int to string
  str= nu1.ToString();

  // Convert string to int
  nu2=System.Int32.Parse(str);

  Debug.Log(nu1);
  Debug.Log(str);
  Debug.Log(nu2);
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 Lo0NuhtiK · Apr 28, 2012 at 09:36 PM 0
Share

Never fails... People mark the answer with a "here's a script for you" area in it ins$$anonymous$$d of another answer that was posted first, and said the same thing in it (or more) as the one that got accepted... lol...

avatar image Fabkins · Apr 28, 2012 at 09:46 PM 0
Share

Sorry didnt notice. Why didnt you just post as an answer rather than a comment?

avatar image Lo0NuhtiK · Apr 28, 2012 at 09:48 PM 0
Share

Because I was only commenting... I meant it about @kolban answer being the one this person should have accepted. Nothin against you, was just talking sh#* about how people on here almost always accept the answer with the biggest code-chunk in it rather than anything else lol

avatar image
2

Answer by kolban · Apr 28, 2012 at 04:25 AM

According to the documentation of TryParse, it wants two parameters, a string and an output int ... see:

http://docs.mono-android.net/?link=T%3aSystem.Int32%2fM%2fTryParse

Did you perhaps mean to use the Parse() method? See:

http://docs.mono-android.net/?link=T%3aSystem.Int32%2fM%2fParse

This however would also not work as in your code you are passing in "price" which is an int and both Parse() and TryParse() expect a String as input.

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 Lo0NuhtiK · Apr 28, 2012 at 04:37 AM 0
Share

Have a look at this. price is always an int. It's not getting turned into a string permanently with ToString() and you don't need to turn it back into an int afterward, because it already is one.

 var price : int = 10 ;
 function Update(){
    if(Input.Get$$anonymous$$ouseButtonDown(0))
       StringToIntTest() ;
 }
 function StringToIntTest(){
    var stringTest : String = price.ToString() ;
    var stringTestAsInt : int = parseInt(stringTest) ;
    Debug.Log("Price + 5 = " + (price + 5)) ;
    price += stringTestAsInt ;
    Debug.Log("Price + price as string and back into an int = " + price) ;
 }

avatar image ObviouslyInsane · Apr 28, 2012 at 05:03 AM 0
Share

Ahh ok, i am needing to convert it to string so I can save it in a string array. Are you saying I don't need to do anything to convert it back to an int after the data is loaded?

EDIT: I just wanted to clarify because I have to do this for a lot of variables and don't want to have to go back and do it all over again.

avatar image Lo0NuhtiK · Apr 28, 2012 at 11:17 AM 0
Share

Well, if you save it AS a string, then what you load will be a string. So, yes, you'll have to convert the loaded string into an int if you want to use it as an int; but that wasn't shown in your main post as being what you were trying to do.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Converting a string into int is rounding off 1 Answer

Download a WWW int? Or convert? 1 Answer

int to string, weird string value? 1 Answer

convert string to int 0 Answers

c# convert 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