• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by EliteHedgehog56 · Aug 06, 2017 at 02:02 AM · textfloat

display the players current health on ui text

I want to display the player's current health level as a number on a ui text component, how would I modify the player's health script to display the health float variable on a ui text

var PointCube : Transform; var spawneffect : Transform; var deatheffect : Transform; var SpawnPoint : Transform; var respawn : boolean = false; var health:float = 90;

function Damage(dmg:float){ health -= dmg; }

function Update (){ if(health <=0){

Instantiate (deatheffect,transform.position,transform.rotation); Instantiate (PointCube,transform.position,transform.rotation);

transform.position = SpawnPoint.position;

 Instantiate (spawneffect,transform.position,transform.rotation);

var audio: AudioSource = GetComponent.(); audio.Play(); audio.Play(44100);

health += 100; }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GamesMaker · Aug 06, 2017 at 02:35 AM

First you need to add var healthText : UI.Text; as a new variable to your script, this will create a new public var for a UIText which allows to you simple assign a UI --> Text Object to your script. After doing so and saving the changes a box should appear in your script properties in the inspector.

alt text

Now Create a new Game Object --> UI --> Text (this will automatically create a canvas if you dont have one selected in the Hierarchy) and assign it to Health UI. After that you can change the content of the Text field in your script with the following code:

HealthText.text = "Health: " + health;

This code would make it display "Health: 90" if the health var is 90. Of course you always need to execute this code when health changes otherwise the health wont update. So either add it to the Update function or a function which is called whenever your character takes damage.

EDIT: Sorry i was mixing up GUI with UI which of course wont work. This way it should work.


example.png (4.5 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EliteHedgehog56 · Aug 06, 2017 at 03:27 AM 0
Share

this looks like what I need but the ui text object wont assign to the health script, what am I doing wrong?

avatar image
0

Answer by $$anonymous$$ · Aug 06, 2017 at 12:33 PM

 var PointCube : Transform; var spawneffect : Transform; var deatheffect : Transform; var SpawnPoint : Transform; var respawn : boolean = false; var health : float = 90; var healthText : UI.Text;
 function Damage(dmg:float){ health -= dmg; }
 function Update ()
 {
     if(healthText)healthText.text = health.ToString();
     if(health <=0)
     {
         Instantiate (deatheffect,transform.position,transform.rotation); Instantiate (PointCube,transform.position,transform.rotation);
         transform.position = SpawnPoint.position;
         Instantiate (spawneffect,transform.position,transform.rotation);
         var audio: AudioSource = GetComponent.(); audio.Play(); audio.Play(44100);
         health += 100;
     }
 }

Hope It Helps :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

X.Text just uses random values 1 Answer

Text UI is not being assigned 2 Answers

Add up updating float 2 Answers

how to edit only specific part of text UI ? 2 Answers

displaying text problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges