V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Felldeadbird
V2EX  ›  C#

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

  •  
  •   Felldeadbird · 2021-02-27 14:53:49 +08:00 · 1681 次点击
    这是一个创建于 1126 天前的主题,其中的信息可能已经有所发展或是发生改变。

    初学 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 就提示没声明。。哭~

    第 1 条附言  ·  2021-02-28 00:12:50 +08:00
    问题解决了。是我犯下一个低级错误。

    游戏有一个全局的变量 g 。 应该 g.world.playerUnit 去调用的。而我自己 new 一个对象。导致出错。
    2 条回复    2021-02-27 16:03:58 +08:00
    EKkoGG
        1
    EKkoGG  
       2021-02-27 15:17:34 +08:00   ❤️ 1
    信息不太够,不过就这样看,可能是创建对象时,unit 有一个默认值或者创建对象时赋值了,而 playerUnit 没有
    cxe2v
        2
    cxe2v  
       2021-02-27 16:03:58 +08:00   ❤️ 1
    看你这个命名能看出来 playerUnit 的定义类型是一个抽象类,甚至可能是个接口,IOC 没法自动创建实例
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2822 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:53 · PVG 22:53 · LAX 07:53 · JFK 10:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.