O_P_2020
O_P_2020

Reputation: 11

Unity C#: type error referencing float from another script

I'm new to Unity, and although I've looked this up a million times, I can't get my script to stop erroring. I am trying to reference a public float (attached to a different object) in a new script - which is simple, I know. I have tried various methods, but this is what I have so far. I keep getting the error that the float (currentHealth) cannot be implicitly converted to 'Health', which I understand but cannot seem to fix. What am I doing wrong?

public class sugarGenerator : MonoBehaviour
{
    public GameObject sugar;
    public GameObject Insulin;
    public Transform generationPoint;
    public float distanceBetween;

    Health PlayerHealth;

    // Update is called once per frame
    public void Awake ()
    {
        GameObject.FindWithTag("Main_Girl_0");
        PlayerHealth = GetComponent<Health>().currentHealth;
    }
}

Upvotes: 1

Views: 190

Answers (1)

Broots Waymb
Broots Waymb

Reputation: 4826

Based on my comment:

PlayerHealth is not a float, it is Health. You cannot assign a float (the type of currentHealth) to a Health.

I'm not familiar with Unity, but perhaps you meant to do:

PlayerHealth = GetComponent<Health>();

Upvotes: 4

Related Questions