• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to fix “Cannot implicitly convert type ‘float’ to ‘UnityEngine.GameObject'” error?

Resolved: How to fix “Cannot implicitly convert type ‘float’ to ‘UnityEngine.GameObject'” error?

0
By Isaac Tonny on 02/04/2023 Issue
Share
Facebook Twitter LinkedIn

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.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:
I’ve tried to make the line of code:
To:
However, it just gave a different error message.

Best Answer:

What the error is telling you is that you’ve got the float 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)
And on this line, you’re telling unity to take the health value and replace the current player object with it…
Since a 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…
In your start method, you get a reference to the Health Script on the player (but not the current health value)…
Then from within your update method, you can use playerHealth.Health to get the current health value.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

c# unity3d
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to stop a thread that has a blocking function from easygui in python

05/04/2023

Resolved: Removing null keys from a json array of objects

05/04/2023

Resolved: How can I generate at compile-time a separate OpenAPI Swagger.json file for each Controller in my ASP.NET project?

05/04/2023

Comments are closed.

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.