在 Unity C# 之前在线定义时出现错误,提示我的字符串为空[重复]

问题描述 投票:0回答:1

整个代码对我来说看起来不错,但我是初学者,所以如果很明显请耐心等待。

我的问题是我收到错误

未将对象引用设置为对象实例

ehealth
是敌人的生命值,这是为了分配史莱姆的统计数据

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class EnemyStats : MonoBehaviour
{
    public TMP_Text Text;

    void Update()
    {
        string ehealth = GameObject.Find("VStorage").GetComponent<VStorage>().ehealth.ToString();

        Text.text = ehealth;
    }
}

我不知道该尝试什么,所以我请求你的帮助。我尝试更改一些变量名称,但没有任何改变

c# unity-game-engine
1个回答
0
投票

您必须将“文本”变量分配给检查器中的 TMP:Drag the TextMeshPro (Text) in this slot

大多数 C# 程序员也使用“camelCase”命名约定:第一个单词以小写字母开头,变量名中所有后续单词以大写字母开头。

© www.soinside.com 2019 - 2024. All rights reserved.