Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 rahra · Sep 05, 2011 at 09:01 AM · guiplayerhealth

Gui Health Display Help!

Hi everyone! I'm really struggling with using the GUI to display my Players health, I know my players health script works because I can see it in the inspector going down when it takes damage but I have no idea how to turn that into a GUI so that when I actually export the game the person playing it can also see the health go down! The script i'm using for my player health is:

var playerHealth : float = 100; var difficulty : int = 5;

function OnTriggerStay(collision : Collider) { if(collision.CompareTag ("Enemy")) { playerHealth -= Time.deltaTime * difficulty; Debug.Log("Enemy is in contact."); } }

Any help is appreciated! I really want it to display as text rather then a health bar!

Comment
Add comment · Show 2
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 CHPedersen · Sep 05, 2011 at 09:07 AM 1
Share

If you don't want an actual health bar, but you just want to display the number, then this is a 5-lines-of-code-task, and you can accomplish it by looking into the Unity GUI controls and the label, particularly.

This is the Unity GUI scripting guide:

http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html

Read it, study the code samples, and you will find that what you want is really easy to do. :P

avatar image rahra · Sep 05, 2011 at 09:29 AM 0
Share

Yeah, easy for most normal people however I struggle with the easiest of scripting, Thanks for answering though =)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DevonJavaScript · Sep 05, 2011 at 09:12 AM

First off you have many options.

  var labelPos : Rect =  Rect(100,100,100,20);
   function OnGUI(){
          GUI.Label(labelPos, "HP:" + playerHealth); 
   }

 This is probably one of the wisest decisions.
 You can put it on an empty game object or your main camera and it will work.
 

Comment
Add comment · Show 4 · 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 rahra · Sep 05, 2011 at 09:28 AM 0
Share

Thanks heaps for that, but it just says that the player health is an unkown identifier, but you've given me an idea of what i'm supposed to be doing so thats a really huge help =) thanks!

avatar image DevonJavaScript · Sep 05, 2011 at 09:39 AM 0
Share

Any time. Sometimes you just need an extra set of eyes. PS: playerHealth references the variable you already have in the other script.

avatar image rahra · Sep 05, 2011 at 09:42 AM 0
Share

Got it working! I needed to add it onto my playerhealth script and add brackets to it! so now it reads:

var playerHealth : float = 100; var difficulty : int = 5; var labelPos : Rect = Rect(100,100,100,20);

function OnTriggerStay(collision : Collider) { if(collision.CompareTag ("Enemy")) { playerHealth -= Time.deltaTime * difficulty; Debug.Log("Enemy is in contact."); } }

function OnGUI(){ GUI.Label(labelPos, "HP:" + (playerHealth)); }

avatar image DevonJavaScript · Sep 05, 2011 at 06:13 PM 0
Share

Yeah. That was the intention.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Restart with GUIText 2 Answers

Taking a hit 3 Answers

Can you make a GUI.Box follow a player's position?(SOLVED) 7 Answers

GUI SCREW UPS! 1 Answer

Deduct health on collision 2 Answers


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