What is wrong with my script
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class HealthBar : MonoBehaviour {
public RectTransform healthTransform;
private float cachedY;
private float minXvalue;
private float maxXValue;
private int currentHealth;
private int CurrentHealth
{
get{ return currentHealth;}
set
{
currentHealth = value;
HandleHealth();
}
}
public int MaxHealth;
public Text healthText;
public Image visualHealth;
public float coolDown;
private bool onCD;
// Use this for initialization
void Start ()
{
cachedY = healthTransform.position.y;
maxXValue = healthTransform.position.x;
minXvalue = healthTransform.position.x - healthTransform.rect.width;
currentHealth = MaxHealth;
onCD = false;
}
IEnumerator coolDownDmg()
{
onCD = true;
yield return new WaitForSeconds (coolDown);
onCD = false;
}
// Update is called once per frame
void Update () {
HandleHealth();
}
private void HandleHealth()
{
healthText.text = "Health: " + currentHealth;
float currentXValue = MapValues(currentHealth, 0, MaxHealth, minXvalue, maxXValue)
//ERROR HERE //ERROR = Unexpected symbol `healthTransform'
healthTransform.localPosition = new Vector3(currentXValue, chachedY);
if(currentHealth > MaxHealth/2)//More than 50%
{
visualHealth.color = new Color32((byte)MapValues(currentHealth, MaxHealth/2, MaxHealth, 255, 0),255,0,255);
}
else//Less than 50%
{
visualHealth.color = new Color32(255, MapValues(currentHealth,0,MaxHealth/2,0,255),0,255);
}
}
void onTriggerStay(Collider other)
{
if (other.name == "Damage")
{
if (!onCD && currentHealth > 0)
{
StartCoroutine(coolDownDmg());
CurrentHealth -= 1;
}
}
if (other.name == "Health")
{
if (!onCD && currentHealth < MaxHealth)
{
StartCoroutine(coolDownDmg());
CurrentHealth += 1;
}
}
}
private void MapValues(float x, float inMin, float inMax, float outMin, float outMax)
{
return(x - inMin) * (outMax - outMin) / (inMax - inMin) + (outMin);
}
}
it's going to be tough to help you without more information about what's wrong.
what's the expected behavior? what's the actual behavior? any error messages? etc.
try formatting the entire code too - any error message will be harder to see since the line numbers in the listing won't match...
i have typed out the error on line 54 (according to web) and it says: Unexpected symbol `healthTransform' the error is on line 55 (according to web)
sorry for bad english
Press F7 in $$anonymous$$onobehavior and tell us the result, if there is any.
Answer by AngryTilde · Nov 09, 2015 at 12:08 PM
I would suggest looking at line 55 (or 54) again, the compiler error is right something isn't right there and it thinks that line 54 and 55 are part of the same statement rather then two separate statements.
Hint: It's something that beginners, amateurs and even professionals sometimes miss.
There's a few other errors through out but I think you will recognise them with the help of the compiler errors (Which it should spout out once the original error is fixed), they are fairly minor ones.
Your answer
![](https://koobas.hobune.stream/wayback/20220612044309im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I can't test the game 0 Answers
Issue referencing a method that exists in a component that is only decided at runtime 1 Answer
How to make a script that could activate a aniamtion by activating a switch? 0 Answers
How do I change the coordinates of the GameObject to the first cube in the generated grid? 0 Answers