Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Demigoth on March 13, 2014, 09:40:38 PM

Title: Assign a variable value to a UILabel
Post by: Demigoth on March 13, 2014, 09:40:38 PM
Hey guys,

So I have these variables in a Player.cs file, like health and gold and a bunch of public variables that constantly change in game, so I was wondering I have a UITexture with a child UILabel on it but I cant seem to find a way to assign the label text this variables value. Any help would be greatly appreciated. I was trying something like this:

  1. public UILabel healthLabel;
  2.  
  3. void Update()
  4. {
  5.      healthLabel.text = Player.health.ToString();
  6. }
  7.  

Any help would be great!

Victor
Title: Re: Assign a variable value to a UILabel
Post by: rain on March 14, 2014, 06:44:15 AM
What is 'Player' in this context? The class itself? An instance?
Your code should work as long as neither 'healthLabel' , 'Player' and 'Player.health' is null.

If Player is actually a class and not an reference to an instance, you should have something in your script like
  1. public Player player;
  2.  
And then assign value to it via inspector or code.
After that, you can easily reference it in your Update method (example full script):
  1. public UILabel healthLabel;
  2. public Player player;
  3.  
  4. void Update()
  5. {
  6.      healthLabel.text = player.health.ToString();
  7. }
  8.  

Cheers