In this post, we will see how to resolve How to fix “Cannot implicitly convert type ‘float’ to ‘UnityEngine.GameObject'” error?
Question:I’m trying to get a float variable from the script
PlayerHealth.csand use it in script
LightLevelbut I get the error message
Cannot implicitly convert type 'float' to 'UnityEngine.GameObject'on line 23. I don’t know what to do to fix it.
I also explain what I’m trying to do near the top of the
LightLevel.csscript in case it’s needed.
Best Answer:What the error is telling you is that you’ve got the
floatand you’re trying to assign it to something that’s not suitable for holding a float (in this case, a
On this line, you’re telling Unity
playeris a GameObject (a regular unity object with a transform, position, name, some scripts attached, etc)
playerobject with it…
GameObject, it can’t hold a float directly.
Note as well that because that’s in the
Start()method, it’s only going to be read once at startup, then never updated again.
More likely you want to do something like this…
playerHealth.Healthto get the current health value.
If you have better answer, please add a comment about this, thank you!