- Home /
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); } } }
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.
'distance' does not exist. You need to create it and assign it a value.
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.
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!
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...
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.
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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Change the color of the string 2 Answers