Getting an Error Message CS1525 for 'new Rect()' Statement

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;
	}
}

In line 55, you have written:

_display.pixelInset = CalculatePosition;

Where CalculatePosition should be called as a method, I’m not sure if that is the problem but errors sometimes accumulate and the console’s info doesn’t help much.

Well I tried something different Josh.

I went into that very line (Line 5), and changed it to look like this:

_display.pixelInset = (new)(CalculatePosition);

Now the error say this:

Assets/Scripts/HUD Classes/VitalBar.cs(63,43): error CS1525: Unexpected symbol )', expecting [‘, {', or type’

If I remember correctly, Pete did something similar when he had to typecast before. Considering I’m still getting used to typecasting, I’m bound to make some errors in ‘punctuation’ in the code’s function. I’ve tinkered with the line this way before, but I’ve never been able to get the punctation right in order to get this very error to go away.

Any suggestions as to what I’m doing wrong with this method?

I may be a little closer with this one:

_display.pixelInset = (new[CalculatePosition]);

Now I get two errors:

Assets/Scripts/HUD Classes/VitalBar.cs(63,40): error CS0178: Invalid rank specifier: expected ,' or ]’

Assets/Scripts/HUD Classes/VitalBar.cs(63,62): error CS1525: Unexpected symbol )', expecting ,‘, or `]’

Just a bit confused. I’ll tinker around with the statement first before trying to call it as a method. Most of the time I can find a way around errors like this if do just that - tinker.

I made another change in the code to get this:

_display.pixelInset = GUITexture(CalculatePosition);

I did it this way because of the _display is defined and used in the file:

private GUITexture _display;
_display = gameObject.GetComponent<GUITexture> ();

And I got this error:

Assets/Scripts/HUD Classes/VitalBar.cs(63,39): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected

So right now I’m not sure what to do. Like I said in a previous reply, I need to do some thinking. I remember having problems when working with other programming languages before, and sometimes it takes the right person with the right answer to come along.

I think you need to spend some time going through a tutorial on coding for C# to understand the syntax. You aren’t going to get anywhere trying things randomly like that.

You call a function like this:

_display.pixelInset = CalculatePosition();

Because you defined it like this:

private Rect CalculatePosition()

And when you call it it does this:

{
        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);
    }

And when it returns, “CalculatePosition()” is replaced with the return value (either one of the “new Rect(…)” statements in the function). I think you’re a little confused about what functions and variables are and how code flow is determined so you should spend some time learning the basics and you’ll have less trouble down the line.

I do admit I haven’t spent enough time in C# Shane. And I do need to start learning to watch what each return statement actually returns, as well as the path of logic that code executes; which parts gets and exited, and so forth.

Tell you truth, I assumed what I was doing would be a good way to learn C#, but it seems I bit off more than I could chew. Right now it’s just best if I read up on my C# books and start watching such programming tutorials on YouTube. That’s worked for me in the past, and it should help here too.

I did some studying of code, and I made the discovery that my error was a simple one.

After tinkering for some time with this statement:

_display.pixelInset = CalculatePosition;

I’ve come to realize I should have tried something that I’ve done before with some codes - end a statement with a pair of opposing parantheses:

_display.pixelInset = CalculatePosition();

After adding the parentheses before the semicolon, the program worked just fine. I’m now wondering how I missed this small detail. But this does show I need to go back and do some tutorials on C#. I’ve got two books on Visual Studio C# 2008, and I do have Visual Studio C# 2010 on disk as ISO image, so I’ve already VS C# 2010 installed on my computer. Best start doing some the books’ exercises ASAP. I hope not to make myself feel foolish again… :wink:

So I now have the complete code:

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;
	}
}