Search Unity

Newbie question: How to make a TextUI display a variable from another script? C#

Discussion in '2D' started by BL4CK0UT19, Aug 21, 2019.

  1. BL4CK0UT19

    BL4CK0UT19

    Joined:
    Aug 21, 2019
    Posts:
    21
    Okay,so this is my first game in unity.
    Im really happy with myself,because im doing everything i can with little to no problems at all.
    But THIS ONE has got me.
    I've been trying for days,and i can't do it.
    I have spent hours and hours tryng to make a simple text UI display a variable from another script,but nothing works. I gave up and came to get help :).

    I really think i don't need to post any code here. All that you have to know is that i have a script called:
    PlayerMovement and i have a variable there "public int life = 5;"

    Now i created a TextUI in canvas and i want it to display this variable and change it over time (when player loses life).
    Looks simple,but i just can't get it to work. Please make it as simple as it can,because i'm not an expert yet.
    Thanks!
     
  2. witylernn

    witylernn

    Joined:
    Feb 1, 2017
    Posts:
    2
    You'll have to add a script to your text object, that grabs the player's health and converts it into text.

    You could also reference the Text element in your player script and update the UI from there whenever the players health changes
     
  3. BL4CK0UT19

    BL4CK0UT19

    Joined:
    Aug 21, 2019
    Posts:
    21
    Thanks,friend. That worked fine!
    Thank you very much.