c# - 使变量的值 A = B unity

标签 c# unity3d

<分区>

这里有人是绞刑架。 我有以下问题。我有两个类(class)。 . 类 ListPainel。 和类项目要求。

在 ListPainel 类中,我有以下内容:

[System.Serializable]
public class Item
{
public GameObject slotPrafab;
public int numeroDeItensDisp;
public int numeroDeItensTotal;
}
public List<Item> itemList;

等等等等......

在 RequirementsItems 类中,我有以下内容: 只有公共(public)变量,以便我可以在检查器统一中更改。

public Sprite imageNewItem;
public Image imageItem;
public Text req;
public string barra;
public int numberReqDisp;
public int numberReqTotal;
public static Item painel;

等等等等......

我需要变量/numberReqDisp 的值; be = 项目数;类 listPainel ... 所以我会这样欺骗:

numberReqDisp = painel.numeroDeItensDisp;

然后发生了什么。在我的团结中。值不会改变。并给出错误“对象引用未设置到对象的实例”

.. 并明确所需的类项目已附加到预制件。

最佳答案

“Object reference not set to an instance of an object”表示没有初始化。你可以那样做。但也不要忘记分配 painel 的值。像那样:

public static Item painel = new Item();
painel.slotPrafab = ...

或者为此定义一个构造函数。

关于c# - 使变量的值 A = B unity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46865950/

相关文章:

c# - 避免在 Unity 中与 self 发生冲突

c# - 在 Unity 5 中使用 .NET 4.5 代码

c# - 套接字在 X 消息后停止接收

c# - .NET CORE 中缺少方法 PasswordDeriveBytes (PBKDF1)

c# - 我可以在 Visual Studio/Code 中测试/运行 Unity C# 吗?

c# - 将数据存储在程序中或文档文件中有区别吗?

c# - Unity3d:CharacterController 不接地;无法连续播放脚步声

c# - 其他对象引发的工作事件时的跨线程操作

c# - 改变数组中的元素

c# - 在 .Net 中使用反射是否会导致性能相当糟糕?