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!
Rob
December 19, 2018, 9:32pm
2
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;
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.
Rob
December 19, 2018, 9:42pm
4
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
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;
}
Rob
December 19, 2018, 9:45pm
6
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
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.
Rob
December 19, 2018, 9:49pm
8
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.
I’ll do it as a property for now! Just doing the basics so far. Learning as I go.
Rob
December 19, 2018, 9:50pm
10
You’ll not forget this one now
Hope you enjoy the rest of the course