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 Kman43759 · Feb 12, 2015 at 03:09 AM · javascripttextguitext

[JS] How to display an Int variable through a GUI Text?? [Resolved]

So I have the variable CurrentMoney which is an Int. And I have a GUI Text. I want the GUI Text to always display what the CurrentMoney is. Before you say I need to "have a concrete question" or "I havent tried enough"... nope nope nope ive tried alot so nope. Please help, thanks :)

CODE : var Wallet : GUIText; var CurrentMoney : int; function Update () { Wallet.text = CurrentMoney.ToString(); }

EDIT: And yes I tried creating a variable for the gui text, does not work var Wallet : GUIText; And then I try to assign the gameobject of the GUIText to the variable and unity is just like "suck my nads" Btw the script is not located under the GUIText, its located under the player

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

Answer by Kman43759 · Feb 12, 2015 at 08:51 PM

I fixed it.

 var Wallet : UnityEngine.UI.Text;
 var CurrentMoney : int;
 
 function Update () {
  Wallet.text = "Wallet: " + CurrentMoney.ToString() + "$";
 }

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
avatar image
1

Answer by Fornoreason1000 · Feb 12, 2015 at 03:24 AM

Hi, so I think i know what you are having trouble with (code always helps, pics too), you don't know how to convert an int to a string? are you aware that GUIText has a string variable called text you can edit? if not read this.

http://docs.unity3d.com/ScriptReference/GUIText-text.html

if you have used it, the its just the problem of converting your int to a string. have you ever heard of the ToString() method that returns a string from any type of object? (its from the System.Object class) if not read this, please know the the documentation is in C# because i cannot find unity script docs on this one but it will serve your purpose.

https://msdn.microsoft.com/en-us/library/system.object.tostring%28v=vs.110%29.aspx

now before you say "but its in C#" , Unity uses Mono which is like .NET, both have a class called Object, the base class for all objects (yes every single one), this class has the method that contains ToString(). which is why you can use it on virtually everything. This applies to all Languages in unity that is: C#, UnityScript and Boo, not sure on the shader languages though.

your problem is solved simply by

 //This will display you're current money, ToString(), 
 //will convert the output of your int to a string
 Wallet.text = CurrentMoney.ToString(); 
 

please know that going the other way around uses the Int.Parse() method thus only works on strings.

Hope it helps

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 Kman43759 · Feb 12, 2015 at 08:36 PM 0
Share

:( The only problem is I do not know how to assign the GUIText gameobject called Wallet, to the variable called Wallet. I tried dragging it onto the variable in the inspector but it will not let me

Theres a error saying the variable wallet has not been assigned.

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

20 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

Related Questions

I can't print a int.ToString() variable in a guiText.text 1 Answer

Gui text, ammo counter 1 Answer

How to set different alignments to differenent lines of text in one field of text 0 Answers

How to display an array of texts on one at a time on button press? 3 Answers

HELP! Make text appear in scene after 5 seconds? 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