Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Kashiki · Nov 17, 2013 at 08:06 PM · changedisplaynumbershealth

Changing Health Display GUI

Here's hoping that this isn't a duplicate.

TL;DR Background Here:

So, I'm doing a test for a game I'm helping to create. I'm testing every component of the game with things called "spaces", something my physics teacher coined. So, the game itself is complicated as hell; the best way for me to learn how to program it is do trial and error and test with a crappy version along the way, effectively getting an idea on how I will process the game when actual development starts next year or so.

What I'm doing, at the moment, is called the "Battlespace"; those with military knowledge should know about that. To put it simply, it is the culmination of all forms and systems of combat in the game, since it is very combat-centric. So far, in the last few days, I've learned a lot. However, that's probably because I have some minimal experience with programming.

TL;DR Background End

Anyways, I've run into a bit of an issue. I was thinking about how one would display enemy health within the GUI; at first, I thought "health bar". But, I don't like those at all, and our Creative Director agrees. I wanted a system where I could display the health of the enemy as a simple integer upon focus on them. Not only that, but I want it to be able to change.

Imagine if you created a calculator on a webpage, and using JavaScript you tell the calculator to do this, this, and this... the result of your actions are shown after you use it, being that the integer is changed right in front of you. I want to know if GUI Text will do that for me, or if there is something better than that.

I would prefer it to be in JavaScript, but use whatever is most comfortable as I could just parse the information I need from it :)

If any pictures/examples of this potential GUI are needed, please tell me. Thank you in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Cherno · Nov 18, 2013 at 12:41 AM

What exactly do you want to know? From what I understand, you just want to display a simple number over a unit that corresponds to the unit's current health.

 private var WorldNamePos : Vector3;
 var Health : float = 100;
 
 function OnGUI()
 {
 WorldNamePos = Camera.main.camera.WorldToScreenPoint(Vector3(transform.position.x, transform.position.y + 0.3, transform.position.z));
                 GUI.Label (Rect (WorldNamePos.x - 50, Screen.height - WorldNamePos.y - 64, 100, 50), Mathf.Round(Health) + "");
 }
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 Kashiki · Nov 18, 2013 at 01:17 AM 0
Share

This is amazing.

So, that world name position is only used for this script (just assu$$anonymous$$g, as it's private). Why is it a vector, though? Is there some sort of acceleration or direction for the display? Just a question; feel free to vilify me if it is obvious.

EDIT: Ok, so apparently I don't have the correct version to use floats.

BCE0023: No appropriate version of 'UnityEngine.GUI.Label' for the argument list '(UnityEngine.Rect, float)' was found.

Any way to change my version.

avatar image Cherno · Nov 18, 2013 at 01:24 AM 0
Share

You can make the WorldNamePos variable public if you like, I just set it to private to it doesn't show up in the Inspector (helps if there are a lot of variables).

It's a Vector3 because it has to be one :D Seriously, this Vector3 is the actual spot in the game world where the GUI.Label is drawn at (but of course since it's called from OnGUI(), it is not actually part of the game world. A Vector3 can just act as coordinate information. After all, that's how transform.position and so on works.

Note that you can also make the Health variable an int if you like, then you could get rid of the $$anonymous$$athf.Round. If just like to have things like Health a flaot so I can work with it more flexibly.

If you feel that the answer has helped you, please remember to mark it as appected.

avatar image Kashiki · Nov 18, 2013 at 01:34 AM 0
Share

Thank you for the help. Things have been fixed, had to set it to a string. Thank you very much.

avatar image Cherno · Nov 18, 2013 at 02:54 AM 0
Share

Oh, I just saw your edit in the first comment. I forgot that GUI:Label HAS to have a reference to a string, even if it empty. The line has to be like this:

     GUI.Label (Rect (WorldNamePos.x - 50, Screen.height - WorldNamePos.y - 64, 100, 50), $$anonymous$$athf.Round(Health) + "");

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

18 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

Related Questions

Health script, save health in between scenes. PlayerPrefs. 2 Answers

How to reduce number of digits in coordinate points? 2 Answers

Text Mesh Pro - Unicodes 0 Answers

GUI Display in game cash help 1 Answer

2D GUI Group moving with players. 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