- Home /
How do i link a GUI text to a variable text which is a integer?
Actually i want to display health in the form of text and i have a variable named Health and i want the the variable's text to be displayed in the GUI, but it has error saying 'cannot convert int to string' what do i do? Here's the code:- #pragma strict
var MaxHealth = 100;
var Health : int;
var healthtext : GUIText;
function Start ()
{
Health = MaxHealth;
healthtext.text = Health;
}
//this not the problem so don't read the code below-
//function ApplyDammage (TheDammage : int){
// Health -= TheDammage;
//
// if(Health <= 0)
// {
// Dead();
// }
//}
//function Dead()
//{
// RespawnMenuV2.playerIsDead = true;
// Debug.Log("Player Died");
//}
//function RespawnStats ()
//{
// Health = MaxHealth;
//}
Its not working. There are no errors but its not working.
Not working means what? And who is this comment for? Note the 'add new comment' button on the lower right of each question. Note that you code is only executed once. If you want your values to change as Health changes, then you will need to execute it every frame, or at least every time Health changes.
Exactly. You just asked us how to correct your error. If you want to make this work during the game, you need to put the healthtext.text = Health.ToString();
line into the Update() method.
Unless you don't specify what your problem is, you won't have a better answer than you already had.
Now its working actually i had put it at two places thanks a lot. No need of update method fisrt i had put it in function start and then in funtion apply damage.
Answer by KiraSensei · Mar 10, 2014 at 01:38 PM
Use this :
healthtext.text = Health.ToString();
Answer by ShadoX · Mar 10, 2014 at 01:38 PM
healthtext.text = Health.toString();
http://www.w3schools.com/jsref/jsref_tostring_number.asp
Hope this helps :)