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
1
Question by Phil 9 · Dec 31, 2010 at 08:41 PM · guiresizehealthbar

Working health script?

Hi I have this script that I have from another question that I asked and was wondering how to make the health bar smaller when I get hit by tagged bullets


public float maxHealth = 100.0f; // Minimum health is 0.0f (dead) public float currentHealth = 100.0f; // Players current health

public Texture2D background = null;

public Texture2D energybar = null;

void OnGUI() {

// Draw the background

GUI.DrawTexture(new Rect(32.0f, 32.0f, 128.0f, 16.0f), background);

// Draw the health/energy bar

GUI.BeginGroup(new Rect(34.0f, 34.0f, 124.0f * (currentHealth / maxHealth), 12.0f)); GUI.DrawTexture(new Rect(0.0f, 0.0f, 124.0f, 12.0f), energybar); GUI.EndGroup(); }

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 Ejlersen · Jan 01, 2011 at 12:17 AM

What do you mean by smaller? Do you mean that the GUI should be smaller? Try editing the last two parameters in the Rect's.

If you mean that when I get hit, I receive 10% of my life, so show only 90%. Then you just need to subtract currentHealth with the amount you lost.

Comment
Add comment · Show 2 · 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 Phil 9 · Jan 01, 2011 at 06:36 AM 0
Share

Yeah that is what I mean but I forgot o put that I have a script but It won't work but I can't post it because I'm on my iPod. So yeah I know what you mean but when I tried it, it didn't work.(i didn't have any GUI on it though)

avatar image Ejlersen · Jan 01, 2011 at 11:32 AM 0
Share

You need to have two textures for it to work. I know it, because that script looks a lot like one I provided a few days ago :)

avatar image
0

Answer by SnStarr · Jan 10, 2015 at 02:27 AM

This is easier. Try this.

     using UnityEngine;
     using UnityEngine.UI;
     using System.Collections;
     
     public class PlayerHealth : MonoBehaviour 
     {
         public float maxHealth; //Your players max Health
         public float curHealth;  //Your players current health
         public Scrollbar healthBar;  //Unity UI Scrollbar Image named healthBar
     
     void Update()
      {
         //Call HealthBar function, pass it a 0 to avoid object reference error
         HealthBar(0); 
      }
        //The HealthBar function
         public void HealthBar(float adj) 
      {
         //Will create a variable named percentile based on the current health divided by the max health multiplied by 100.
         float healthPercentile = ((float)curHealth / (float)maxHealth) * 100f; 
 
 //Will rescale a scrollbar set to FILL type by the percentile variable multiplied by 100.
         healthBar.size = healthPercentile / 100f; 
      }



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

1 Person is following this question.

avatar image

Related Questions

Textarea rezing in height by text 1 Answer

Gui Resize 1 Answer

Shrink GUI 1 Answer

GuiTexture above 3D objects 1 Answer

Detect if any GUI is being displayed on-screen? 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