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 bpears · Jan 14, 2013 at 04:05 AM · javascripthealth barhealth

How can I change this script to use GUITexture instead of GUI number, for health display?

Goal is to make so that health gets subtracted, and results in new GUItexture every 5% of health drop(as a health bar), but currently, I have a number GUI set up. OR if a scaling bar is more optimized, I may do that.

 @script ExecuteInEditMode()
 
 var hitPoints : int;
 var painSound : AudioClip;
 var die : AudioClip;
 var deadReplacement : Transform;
 var mySkin : GUISkin;
 var explShake : GameObject;
 private var radar : GameObject;
 private var maxHitPoints : int;
 var damageTexture : Texture;
 private var time : float = 0.0;
 private var alpha : float;
 private var callFunction : boolean = false;
 
 function Start(){
     maxHitPoints = hitPoints;
     alpha = 0;
 }
 
 function Update(){
     if (time > 0){ 
         time -= Time.deltaTime;
     }
     alpha = time;
 }
 
 function PlayerDamage (damage : int) {
     if (hitPoints < 0.0)
         return;
 
         // Apply damage
         hitPoints -= damage;
         audio.PlayOneShot(painSound, 1.0 / audio.volume);
         time = 2.0;        

 
 
 function OnGUI () {
     GUI.skin = mySkin;
     GUI.contentColor = Color.red;
     GUI.Label (Rect(40, Screen.height - 50,60,60)," Health: ");
     GUI.Label (Rect(100, Screen.height - 50,60,60),"" +hitPoints);
     
     GUI.color = Color(1.0, 1.0, 1.0, alpha); //Color (r,g,b,a)
     GUI.DrawTexture(new Rect(0,0,Screen.width, Screen.height), damageTexture);
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
1
Best Answer

Answer by Hivemind · Jan 14, 2013 at 05:25 AM

I recommend creating an actual GUITexture in your scene instead of using GUI.DrawTexture. You can also store a rectangle for the current health bar instead of making one every OnGUI call (every frame). I would do this for all rectangles in OnGUI.

To update your health bar, I would do the following ...

 var MAX_HITPOINTS = 100;
 var healthBarOrigWidth : int;
 var healthBar : GUITexture;
 
 function PlayerDamage (damage : int) {
     //Somewhere in this function
     healthBar.width = healthBarOrigWidth * (hitPoints/MAX_HITPOINTS);
 }

This way you don't create rectangles every frame in the OnGUI function. The health bar updates when PlayerDamage() is called.

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 bpears · Jan 21, 2013 at 01:32 AM 0
Share

I am getting this error: $$anonymous$$issingFieldException: Field 'UnityEngine.GUITexture.width' not found

avatar image Hivemind · Jan 21, 2013 at 03:58 AM 0
Share

Generally, you can figure out your problem when it is as simple as a variable/property not found.

http://docs.unity3d.com/Documentation/ScriptReference/GUITexture.html

So you can check on the documentation of GUITexture and see if it exists. In our case, it doesn't. It is inside pixelInset. So basically, someGuiTexture.pixelInset.width.

Hope this helps.

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

9 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Health script and damage script dont work. 2 Answers

Where too learn unityscript? 3 Answers

Save Player Health and stats 1 Answer

Semicolon error using JS 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