I think your issue here is in your Start
method. You are using GameObject.FindGameObjectWithTag
, if we look at the documentation for that method we can see that this returns a GameObject
, it doesn’t return a Player
object, which is what would have the member property healthAsPercentage.
Looking at a code example I have at this end, the code is slightly different.
The player variable is of Type Player
;
Player player;
and is initialised in the Start
method with this;
player = FindObjectOfType<Player>();
Now it could be that this is from a later lecture, but the issue is pretty much the same, you have a casting issue. Note, find by type should also be safer than by tag as it will be less prone to typos or not being applied.
Also, it may just be because you are using a different version of Unity and there have been some API changes, but I can only see (I didn’t spend a lot of time searching) GameObject.FindWithTag
and GameObject.FindGameObjectsWithTag
(note the plural), whereas you are calling FindGameObjectWithTag
.
I’m guessing you are on lecture 29? Assuming so, if you look under Resources and click on the Lecture Project Changes link it will open a browser tab with the GitHub repository for that specific section and you can see the code Ben uses at this stage;
See also;