【问题标题】:Unity C#: type error referencing float from another scriptUnity C#:类型错误引用另一个脚本中的浮点数
【发布时间】:2023-03-13 19:17:01
【问题描述】:

我是 Unity 的新手,虽然我已经查了上百万次,但我无法让我的脚本停止出错。我正在尝试在新脚本中引用公共浮动(附加到不同的对象) - 我知道这很简单。我尝试了各种方法,但这是我迄今为止所拥有的。我不断收到 float (currentHealth) 无法隐式转换为“Health” 的错误,我理解但似乎无法修复。我做错了什么?

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;
    }
}

【问题讨论】:

  • PlayerHealth 不是float,它是Health。您不能将floatcurrentHealth 的类型)分配给Health。我对 Unity 不熟悉,但您可能是打算做 PlayerHealth = GetComponent&lt;Health&gt;(); 吗?
  • 如果您还向我们展示了 Health 课程将会有所帮助。
  • @BrootsWaymb 就是这样!不敢相信我没有发现!

标签: c# visual-studio unity3d reference floating-point


【解决方案1】:

根据我的评论:

PlayerHealth 不是float,而是Health。您不能将floatcurrentHealth 的类型)分配给Health

我不熟悉 Unity,但也许您打算这样做:

PlayerHealth = GetComponent<Health>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2018-01-11
    • 2019-10-19
    相关资源
    最近更新 更多