Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by gamer165 · Jan 07, 2018 at 11:51 PM · javascriptscripting problemtextintegerscritping

How do you change UI Text to an int?

I am trying to make a statistic menu in my game which will show you stats about your progress in the game. I am trying to make a text that will tell you how much time you have played the game.

 #pragma strict
 import UnityEngine.UI;
 
     static var gamesPlayed : int;
     var gamesPlayedText : UI.Text;
         
 function Start () {
     
     gamesPlayed = gamesPlayedText.ToInt();
     gamesPlayedText = GetComponent(UI.Text);
     gamesPlayedText.text = PlayerPrefs.GetInt("gamesPlayedText",0).ToString();
 }
 
 function Update () 
 {
         
 
             
             PlayerPrefs.SetInt ("gamesPlayedText", gamesPlayed);
             gamesPlayedText.text = gamesPlayed.ToString();        
 }



 

The problem with this script is that I want it to set the gamesPlayed variable to the gamesPlayedText variable so it won't go back to 0 whenever I leave the game and go back in. I know that there is a ToString() but when I try ToInt() it gives me an error that it doesn't exist. So Is there a way to make a UI Text an integer? Please answer and Thanks in advance.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by MaxGuernseyIII · Jan 13, 2018 at 07:55 PM

In C# it would be

 int.Parse(gamesPlayedText.text);

Here's an answer provided to the last person who asked this question as pertains to UnityScript.

https://answers.unity.com/questions/37756/how-to-turn-a-string-to-an-int.html

I don't know why you are punishing yourself with UnityScript.

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 Content1of_akind · May 05, 2019 at 09:54 PM 0
Share

Appreciate it

avatar image
1

Answer by Mercbaker · Jan 08, 2018 at 12:22 AM

 gamesPlayed = Integer.parseInt(gamesPlayedText.text);
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 gamer165 · Jan 08, 2018 at 04:36 PM 0
Share

I tried this code, but it gave me an error $$anonymous$$ indetifier: Integer

avatar image
0

Answer by lspence812 · Jan 11, 2018 at 09:32 PM

Use int.parseInt(gamesPlayedText.text):

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 gamer165 · Jan 13, 2018 at 07:42 PM 0
Share

I am getting the error ‘parseInt’ is not a member of ‘int’.

avatar image
0

Answer by KingKline · May 23, 2020 at 05:22 AM

intvariable = int.Parse(txtvariable.text);

Replace "intvariable " and "txtvariable"

Comment
Add comment · 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

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

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

NullReferenceException: Object Reference not set to an instance of an object 1 Answer

Level up system problem 0 Answers

convert this 3 line code to c# ? i cant figure it out? 1 Answer

show textwindow on mouseclick 0 Answers

Infinite floor with object as terrain. 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