C#这个语法调用为什么会报错?

2021-02-27 14:53:49 +08:00
 Felldeadbird

初学 C#,直接上手项目。然后碰到这个问题。

//dll 引用的代码[从元数据]
public class WorldMgr : Il2CppSystem.Object
{

	public WorldMgr();
    
	public WorldMgr(System.IntPtr value);
	
	public WorldUnitMgr unit { get; set; }
	
	public WorldUnitBase playerUnit { get; set; }

}

//我编写的调用类方法
WorldMgr wm = new WorldMgr();

Debug.Log( wm.unit )  //√ 可以正常返回类型

Debug.Log( wm.playerUnit )  //× 提示 Object reference not set to an instance of an object

折腾了快 2 天了。不知道哪里出错了。。头疼。

我看 WorldUnitMgr 也没声明,为什么他可以。而我的 playerUnit 就提示没声明。。哭~

1716 次点击
所在节点    C#
2 条回复
EKkoGG
2021-02-27 15:17:34 +08:00
信息不太够,不过就这样看,可能是创建对象时,unit 有一个默认值或者创建对象时赋值了,而 playerUnit 没有
cxe2v
2021-02-27 16:03:58 +08:00
看你这个命名能看出来 playerUnit 的定义类型是一个抽象类,甚至可能是个接口,IOC 没法自动创建实例

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/756738

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX