Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2208.00元
莫名其妙的报错
克莱宝宝发起了问答2018-12-28
4
回复
362
浏览
//每次回到unity就会报错,但是运行时没有异常,赋值都赋上去了,能正常游戏,不知为什么会报错
//NullReferenceException: Object reference not set to an instance of an object
//Reflecter ..ctor () (at Assets/Scripts/Towers/Reflecter .cs:7)





public class TowerAttr
{
   public int Hp;
   public string DesEff;
    public TowerAttr(int hp,string deseff)
    {
        Hp = hp;
        DesEff = deseff;
    }
}

public class TowerAttrFactroy
{
    public Dictionary<int, TowerAttr> TowerAttrDict = new Dictionary<int, TowerAttr>();
    public TowerAttrFactroy()
    {
        TowerAttrDict.Add(0, new TowerAttr(300, "tyellow"));
        TowerAttrDict.Add(1, new TowerAttr(400, "tred"));
        TowerAttrDict.Add(2, new TowerAttr(350, "tgreen"));
        TowerAttrDict.Add(3, new TowerAttr(300, "tblue"));
        TowerAttrDict.Add(4, new TowerAttr(300, "tindigo"));
        TowerAttrDict.Add(5, new TowerAttr(300, "tgreen"));
        TowerAttrDict.Add(6, new TowerAttr(300, "tyellow"));
        TowerAttrDict.Add(7, new TowerAttr(300, "tblue"));
        TowerAttrDict.Add(8, new TowerAttr(300, "tpurple"));
        TowerAttrDict.Add(9, new TowerAttr(1000, "tgrey"));
        TowerAttrDict.Add(10, new TowerAttr(800, "tgrey"));
        TowerAttrDict.Add(11, new TowerAttr(2000, "tgrey"));
        TowerAttrDict.Add(12, new TowerAttr(200, "tblack"));
    }
    public TowerAttr GetTowerAttr(int id)
    {
        return TowerAttrDict[id];
    }
}




public class TowerFather:MonoBehaviour
{
    protected int Hp;
    private int TotalHp;
    protected string DesEffect;
    private bool secondget = false;
    private Slider BloodSlider;
    public TowerFather(TowerAttr towerAttr)
    {
        Hp = towerAttr.Hp;
        DesEffect = towerAttr.DesEff;
    }
}

public class Reflecter : TowerFather
{
//报错位置    public Reflecter() : base(GameManager.Instance.factroyManager.TowerattrFactroy.GetTowerAttr(9))
    {
        Debug.Log(GameManager.Instance.factroyManager.TowerattrFactroy.GetTowerAttr(9).DesEff);
    } 
}



public class GameManager : MonoBehaviour {
    #region 单例
    private static GameManager _instance;
    public static GameManager Instance
    {
        get
        {
            return _instance;
        }        
    }
    #endregion
    public FactroyManager factroyManager;
    public GameController gameController;

    //信息

    private void Awake()
    {
   
        _instance = this;
        factroyManager = new FactroyManager();
        gameController = GameObject.Find("bg").GetComponent<GameController>();
    }
}


 

所有回复
  • 老师_Trigger 2018-12-28

    同学你好,重启下Unity看问题是否存在,如果还存在把报空那一行输出一下,看具体哪个报空,要注意实例化的先后顺序,保证这个对象是在单例之后实例化的。

    • 克莱宝宝 2018-12-28

      游戏功能正常,而且在点击运行之前就会报错,重启unity也报错。
      我是想用设计模式中那个享元模式给塔属性赋值,每个塔有很多相同对象,需要赋值的内部变量只有一个int类型和string类型的变量,请问这样可以节约内存吗

      (1) 回复
    • 老师_Trigger 2018-12-29

      回复 @ 克莱宝宝: 同学用于学习是可以的,但实际节约不了多少,除非数据非常庞大,我们只在客户端且一个游戏场景中的话不会有很明显变化。

      (1) 回复
    还有-3条回复,点击查看
    你还没有登录,请先登录注册
  • 克莱宝宝 2018-12-31

    谢谢老师

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!