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 SantiN90 · Nov 09, 2010 at 05:34 PM · guiscoreprogress-baraccuracyhealth

Creating a Lifebar with simple GUI in C#

Hi there, I'm pretty new to Unity. I was trying to make a lifebar for my player with Unity's GUI through code in C#.

The maximun life is 200 and the minimum 0. I've already got the damage sorted out practically, but I wanted to display it in a simple lifebar.

I want to find a way to make a GUI bar in the top-left side of the screen that is represented as CurrentLife/TotalLife (TotalLife should be the max size, and should have to be another GUI as a background texture).

There should be a string "LIFE" in the middle of the bar. Also, I want to make the bar and the string to shake when a bullet hits the player.

If you could help me, I'd also like to make a label that shows how many enemies I have killed (I use the Destroy.GameObject thing) in the top right corner of the screen. And, as a player is killed, there should be a Points Label that increases by 20 everytime I kill an enemy (as in "SCORE: 0", I kill an enemy "SCORE: 20", another, "SCORE: 40").

And, to end this, I wanted to add an "ACCURACY" label under the above (as in ShotsThatHitAnEnemy/ShotsMade).

If anyone could help me in any of this, It'd be much appreciated.

Thanks in advance! :)

Comment
Add comment · Show 7
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 skovacs1 · Nov 09, 2010 at 06:06 PM 0
Share
  1. You're asking for a lot in one question, much of which is covered in tutorials and other questions that you would have found if you'd have looked. 2. You haven't included any example of what you've tried already. Essentially you are asking someone to write your code for you. If you don't want to give this impression, try to get it working and then ask about the parts that aren't working for you, including what you've already tried.

avatar image skovacs1 · Nov 09, 2010 at 06:10 PM 1
Share

For example, search 'health bar' (http://answers.unity3d.com/search?q=health+bar) and there are plenty of answers. For the rest of the gui elements, you simply need to store some variables in your gui script and update them as things change. Shaking the GUI is a little more complex and if you asked about that specifically in it's own question, I'd be glad to answer, but simply put, you'd change the positions of your GUI objects over a period of time.

avatar image SantiN90 · Nov 09, 2010 at 06:18 PM 0
Share

Yeap, you are right. Thanks anyways.

avatar image SantiN90 · Nov 09, 2010 at 06:21 PM 0
Share

The thing is, there is no much help in C#. And UnityAnswers search engine is not much of a friend for that matter.

avatar image skovacs1 · Nov 09, 2010 at 06:32 PM 0
Share

Honestly, converting the js to C# is super easy. $$anonymous$$ostly, you just have to move some keywords around. Check out this link if you're having difficulties bridging the gap (http://answers.unity3d.com/questions/5507/what-are-the-syntax-differences-in-c-and-javascript)

Show more comments

3 Replies

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

Answer by SantiN90 · Nov 09, 2010 at 08:30 PM

Okay, I've been coding a little bit and this is what I got

using UnityEngine; using System.Collections;

public class UserInterfaceGraphics : MonoBehaviour {

public Rect lifeBarRect; public Rect lifeBarLabelRect; public Rect lifeBarBackgroundRect; public Texture2D lifeBarBackground; public Texture2D lifeBar;

//private float damage = 10;

private PlayerVitalData PVDScript; public GameObject PlayerData;

private float LifeBarWidth = 300f;

// Use this for initialization void Start(){ instance = this; PVDScript = PlayerData.GetComponent("PlayerVitalData") as PlayerVitalData;

} // Update is called once per frame

public static UserInterfaceGraphics instance;

void OnGUI() {

 instance.lifeBarRect.width = LifeBarWidth * (PVDScript.life/200);
 instance.lifeBarRect.height = 20;

 instance.lifeBarBackgroundRect.width = LifeBarWidth;
 instance.lifeBarBackgroundRect.height= 20;

 GUI.DrawTexture(lifeBarRect, lifeBar);
 GUI.DrawTexture(lifeBarBackgroundRect, lifeBarBackground);

 GUI.Label(lifeBarLabelRect, "LIFE");



}

}

The Life Bar or Health Bar or whatever now works like wonders. I just got hurried up and made questions too soon, instead of searching before.

I hope it helps anyone down the road.

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 M4R5 · Jul 29, 2013 at 09:05 PM 0
Share

"The type or namespace name `PlayerVitalData' could not be found. Are you missing a using directive or an assembly reference?" <-- not declared.

avatar image
0

Answer by Cas Unity3D · Nov 14, 2012 at 02:56 PM

Use GUI.Box instead. Define the lenght of the box using the amount of life left. You have to call the variables from your health script. You have to make assign your texture in the new style.

For example:

 using UnityEngine;
 using System.Collections;
 
 public class UserInterfaceGraphics : MonoBehaviour {
 
 public float currentLife;
 
 public GUIStyle : myGUIStyle;
 
 Void OnGUI () {
 
 //For example you have 100 life’s maximum.
 
 GUI.Box(new Rec(10, 10, 0.001 * Screen.width * currentlife,  0.1 * Screen.heigth), “LIFE”, myGUIStyle);
 
 }
 
 }

I hope this is helpfull.

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
avatar image
0

Answer by angelonit · Mar 16, 2014 at 12:45 PM

Great script!

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

need help with GUI healthbar 1 Answer

Inverse health bar 2 Answers

Adding a score progress bar 1 Answer

Health bar by percentage? (not drawing a box) 4 Answers

Health Bar Centering 4 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