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 scriptPlayerHealth.cs
and use it in script LightLevel
but 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.cs
script in case it’s needed.LightLevel.cs
:PlayerHealth.cs
:Best Answer:
What the error is telling you is that you’ve got thefloat
and you’re trying to assign it to something that’s not suitable for holding a float (in this case, a GameObject
)On this line, you’re telling Unity
player
is a GameObject (a regular unity object with a transform, position, name, some scripts attached, etc)player
object with it…player
is a 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.Health
to get the current health value.If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com