简体   繁体   English

Unity C#:引用来自另一个脚本的浮点数的类型错误

[英]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.我是 Unity 的新手,虽然我已经查阅了一百万次,但我无法让我的脚本停止出错。 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.我不断收到错误,即float (currentHealth) 无法隐式转换为 'Health' ,我理解但似乎无法修复。 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;
    }
}

Based on my comment:根据我的评论:

PlayerHealth is not a float , it is Health . PlayerHealth不是float ,它是Health You cannot assign a float (the type of currentHealth ) to a Health .您不能将floatcurrentHealth的类型)分配给Health

I'm not familiar with Unity, but perhaps you meant to do:我不熟悉 Unity,但也许您打算这样做:

PlayerHealth = GetComponent<Health>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM