Getting a weird error

When importing the script I’m getting this error:

Assets/Camera & UI/PlayerHealthBar.cs(23,33): error CS0019: Operator /' cannot be applied to operands of typemethod group’ and `float’

No idea what this means, gonna try googling and fixing it but thought I’d post it here first!

Hi Joseph,

Could you please copy/paste your script into your post, and apply the code formatting characters before and after, which corresponds to this error.


See also;

1 Like

I’ll try, first time copy/pasting code in here!

void Update()
{
    float xValue = -(player.healthAsPercentage / 2f) - 0.5f;
    healthBarRawImage.uvRect = new Rect(xValue, 0f, 0.5f, 1f);
}

Getting the error on the “float XValue… etc” line. More specifically from within the brackets.

Hmmm, I have the same at this end, it’s been a long time since I have looked at this project.

It’s the division it isn’t liking due to the float, yet your code is the same as mine.

Can you copy/paste your Player.cs script also please - oh, and spot on either the above :slight_smile:

1 Like

Thanks for amazingly quick replies, really appreciate it!

copy/pasting my Player.cs script below:

[SerializeField] float maxHealthPoints = 100f;

float currentHealthPoints = 100f;

public float healthAsPercentage()
{
    return currentHealthPoints / (float)maxHealthPoints;
}

There’s your problem.

This should be a property as opposed to a method, note the () on the end, here’s what I had;

public float healthAsPercentage
{
    get { return currentHealthPoints / maxHealthPoints; }
}

Note, you don’t need the additional cast for maxHealthPoints either.

Update to the above and see if this error goes away :slight_smile:

1 Like

It fixed it! Thank you so much. I must have gotten over-excited and forgotten the property and just auto-defaulted back to typing it as a method. :slight_smile:

1 Like

hehe, you’re more than welcome.

You could have it as a method, if you really wanted to, but then in your first code example you’d need to store it’s return value in a variable before doing the division;

void Update()
{
    float xValue = -player.healthAsPercentage;
    xValue = xValue / 2f - 0.5f;
    healthBarRawImage.uvRect = new Rect(xValue, 0f, 0.5f, 1f);
}

Kinda messy though.

1 Like

I’ll do it as a property for now! :slight_smile: Just doing the basics so far. Learning as I go.

1 Like

You’ll not forget this one now :wink:

Hope you enjoy the rest of the course :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms