[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 :slight_smile:

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

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.


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.

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

I fixed it.

var Wallet : UnityEngine.UI.Text;
var CurrentMoney : int;

function Update () {
 Wallet.text = "Wallet: " + CurrentMoney.ToString() + "$";