I’ve been doing more tutorials on BurgZerg Arcade’s Hack and Slash RPG tutorial playlist, and believe I’ve gotten further with correcting some errors in my code.
But the problem is with my VitalBar.cs file again. I’ve made to progress from my post last night, so the coding is bit different than it was.
A new line in VitalBar.cs now reads this way:
return new Rect (xPos, yPos, _curBarLength, _display.pixelInset.height);
And I got an error from this line:
Assets/Scripts/HUD Classes/VitalBar.cs(63,26): error CS0428: Cannot convert method group CalculatePosition' to non-delegate type UnityEngine.Rect’. Consider using parentheses to invoke the method
It appears I need to add more to the line to either typecast or overload (I’m still getting used to the terminology here). But I know that more needs to be added to the line to make it work. I just don’t know how to fix it.
I’ll post the VitalBar.cs below. I’m certain someone will know how to fix - I just don’t have the knowledge yet of what to do.
VitalBar.cs
using UnityEngine;
using System.Collections;
public class VitalBar : MonoBehaviour {
public bool _isPlayerHealthBar; // This boolean value tells us if this is the player health bar or the mob health bar
private int _maxBarLength; // This is how large the vital bar can be if the target is at 100% health
private int _curBarLength; // This is the current length of the vital bar
private GUITexture _display;
// Use this for initialization
void Start () {
_isPlayerHealthBar = true;
_display = gameObject.GetComponent<GUITexture> ();
_maxBarLength = (int)_display.pixelInset.width;
OnEnable ();
}
// Update is called once per frame
void Update () {
}
// This method is called when the game object is enabled
public void OnEnable(){
if (_isPlayerHealthBar) {
ToggleDisplay(false);
Messenger<int, int>.AddListener ("Player health update", OnChangeHealthBarSize);
} else {
Messenger<int, int>.AddListener ("Mob health update", OnChangeHealthBarSize);
Messenger<bool>.AddListener ("Show mob vital bars", ToggleDisplay);
}
}
// This method is called when the game object is disabled
public void OnDisable(){
if (_isPlayerHealthBar) {
Messenger<int, int>.RemoveListener ("Player health update", OnChangeHealthBarSize);
} else {
Messenger<int, int>.RemoveListener ("Mob health update", OnChangeHealthBarSize);
Messenger<bool>.RemoveListener ("Show mod vital bars", ToggleDisplay);
}
}
// This method will calculate the total size of the health bar in relation to the % of health the target has left
public void OnChangeHealthBarSize(int curHealth, int maxHealth){
//Debug.Log ("We heard an event: curHealth = " + curHealth + " maxHealth = " + maxHealth);
_curBarLength = (int)((curHealth / (float)maxHealth) * _maxBarLength); // This calculates the current bar length based on the player's health %
//_display.pixelInset = new Rect (_display.pixelInset.x, _display.pixelInset.y, _curBarLength, _display.pixelInset.height);
_display.pixelInset = CalculatePosition;
}
// Setting the health bar to the player or mob
public void SetPlayerHealthBar(bool b){
_isPlayerHealthBar = b;
}
private Rect CalculatePosition(){
float yPos = _display.pixelInset.y / 2 - 10;
if (!_isPlayerHealthBar) {
float xPos = (_maxBarLength - _curBarLength) - (_maxBarLength / 4 + 10);
return new Rect (xPos, yPos, _curBarLength, _display.pixelInset.height);
}
return new Rect (_display.pixelInset.x, yPos, _curBarLength, _display.pixelInset.height);
}
private void ToggleDisplay(bool show){
_display.enabled = show;
}
}