首页 > 解决方案 > C# 在脚本的另一部分中使用使用 Class 创建的变量

问题描述

尝试在函数中使用在脚本开头创建的带有类的变量。错误显示“当前上下文中不存在名称 'celula'”。

假设 celula.Name 必须返回“Cel1”。

public class ScreenC : MonoBehaviour
{
    public void Start()
    {
        Cel celula = new Cel("Cel1", 30);
    }
    public class Cel
    {
        public string Name
        {
            get;
        }
        public int ATP
        {
            get;
            set;
        }
        public Cel(string nm, int atp)
        {
            this.Name = nm;
            this.ATP = atp;
        }
    }
    public void ADDTHESE()
    {                 //here it seems to be the error
        GameObject.Find(celula.Name).GetComponent<Text>().text = "ATP = 50";
    }

}

标签: c#functionclassunity3d

解决方案


推荐阅读