V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Felldeadbird
V2EX  ›  C#

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

  •  
  •   Felldeadbird · Feb 27, 2021 · 2338 views
    This topic created in 1895 days ago, the information mentioned may be changed or developed.

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

    Supplement 1  ·  Feb 28, 2021
    问题解决了。是我犯下一个低级错误。

    游戏有一个全局的变量 g 。 应该 g.world.playerUnit 去调用的。而我自己 new 一个对象。导致出错。
    2 replies    2021-02-27 16:03:58 +08:00
    EKkoGG
        1
    EKkoGG  
       Feb 27, 2021   ❤️ 1
    信息不太够,不过就这样看,可能是创建对象时,unit 有一个默认值或者创建对象时赋值了,而 playerUnit 没有
    cxe2v
        2
    cxe2v  
       Feb 27, 2021   ❤️ 1
    看你这个命名能看出来 playerUnit 的定义类型是一个抽象类,甚至可能是个接口,IOC 没法自动创建实例
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3484 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 11:44 · PVG 19:44 · LAX 04:44 · JFK 07:44
    ♥ Do have faith in what you're doing.