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 spiralfire11 · Aug 26, 2013 at 02:57 PM · c#colordamagedropdownhealthbar

Healthbar Color change movement

I'm trying to make my Healthbar cahnge color at a point and make my healthbar get falldamage but I keep getting script errors. The falldamage is supposed to come on collision, but it won't. Here the new the script:

 using UnityEngine;
 using System.Collections;
  
 public class PlayerHealthBar : MonoBehaviour {
  
 GUIStyle style = new GUIStyle();
 Texture2D texture;
 Color redColor = Color.red;
 Color greenColor = Color.green;
  
 public int curHealth = 100;
 public int maxHealth = 100; 
 void Start()
 {
 texture = new Texture2D(1, 1);
 texture.SetPixel(1, 1, greenColor);
 }
  

void Update() { AddjustCurrentHealth(0); } public void AddjustCurrentHealth(int adj) { curHealth += adj;

 if(curHealth < 0)
 {     
 curHealth = 0;
 }
 
 if(curHealth > maxHealth)
 {
 curHealth = maxHealth;
 }
     
 if(maxHealth < 1)
 {
 maxHealth = 1;
 }

if (curHealth > 50) { texture.SetPixel(1, 1, greenColor); } if(curHealth < 10) { Application.LoadLevel ("death"); } if (curHealth < 50) { texture.SetPixel(1, 1, redColor); } }

public void OnGUI() {

texture.Apply();

style.normal.background = texture; GUI.Box(new Rect(10, 10, 1000, 20), new GUIContent(""), style); if (curHealth - 10) { GUI.Box(new Rect(10, 10, 1000-100, 20), new GUIContent(""), style);
}else { GUI.Box(new Rect(10, 10, 1000+10, 20), new GUIContent(""), style); } } }

Comment
Add comment · Show 3
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 ArkaneX · Aug 26, 2013 at 04:09 PM 0
Share

Does OnTriggerEnter is ever called? You can test it by debugging or by simply putting Debug.Log at the beginning. If it is not called, then maybe you don't have rigidbody attached to any of colliding objects?

If this method is called, then please check what is the value of distance variable.

avatar image LemonLake · Aug 26, 2013 at 05:03 PM 0
Share

'distance' does not exist. You need to create it and assign it a value.

avatar image spiralfire11 · Aug 26, 2013 at 09:55 PM 0
Share

and also, it says I can't convert UnityEngine.Color to 'float'. That's for the color change in the healthbar. Both red and green.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jona-Marklund · Aug 26, 2013 at 10:19 PM

Hello, for the color part GUI.Box doesn't use a color but rather a texture, also healthBarLenght is a float so you can't change a color using that float.

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour {
 
     GUIStyle style = new GUIStyle();
     Texture2D texture;
     Color redColor = Color.red;
     Color greenColor = Color.green;
 
     public int curHealth = 100;
 
     void Start()
     {
         texture = new Texture2D(1, 1);
         texture.SetPixel(1, 1, greenColor);
     }
 
     private void Update()
     {
 
         if (curHealth > 50)
         {
             texture.SetPixel(1, 1, greenColor);
         }
 
         if (curHealth < 50)
         {
             texture.SetPixel(1, 1, redColor);
         }
     }
 
     public void OnGUI()
     {
 
         texture.Apply();
 
         style.normal.background = texture;
         GUI.Box(new Rect(100, 200, 100, 100), new GUIContent(""), style);
     }
 }

Here's a simple script that creates a texture and then draws a GUI.Box which can change color depending upon "curHealth" that you can take apart and put into your own script.

Good luck!

Comment
Add comment · Show 6 · 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 spiralfire11 · Aug 27, 2013 at 07:08 PM 0
Share

Thank you very much!

avatar image spiralfire11 · Aug 27, 2013 at 09:03 PM 0
Share

it works but the health doesnt go down like in my old script. I think its because the GUI box isn't set lower down when the health goes down...

avatar image Jona-Marklund · Aug 27, 2013 at 09:31 PM 0
Share

If you change the last line to

 GUI.Box(new Rect(100, 200, curHealth, 100), new GUIContent(""), style);

It'll shrink the health bar, the rest of the functionality should be pretty much copy paste able from your old code.

avatar image spiralfire11 · Aug 28, 2013 at 12:09 AM 0
Share

It won't work cuse it says can't convert type int to bool since my curHealth is an int. And just switching to bool messes up the script.

avatar image spiralfire11 · Aug 28, 2013 at 12:18 AM 0
Share

NV$$anonymous$$ what I said

Show more comments

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Change the color of the string 2 Answers

Accessing color presets in c# script. 1 Answer

Change color of all buttons listed in array 3 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