Author Topic: Assign a variable value to a UILabel  (Read 1841 times)

Demigoth

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 18
    • View Profile
Assign a variable value to a UILabel
« 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

rain

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 79
    • View Profile
Re: Assign a variable value to a UILabel
« Reply #1 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