MonoBehaviour 类的问题

1.错误代码

今天写了一串代码,不知道那里错了

public class UIPetAttributeView  : MonoBehaviour
{
    public UIAttr mPetAttack;         //攻击
    public UIAttr mPetDefense;        //防御
    public UIAttr mPetLife;           //生命
    public UIAttr mPetCrit;           //暴击
    public UIAttr mPetRate;           //暴击倍率
    public UIAttr mPetDodge;          //闪避
    public GameObject mRoot;
    public UIPetAttributeView(GameObject go)
    {
        this.mRoot = go;
        mPetAttack = UIAttr.CreateUIAttr(this.mRoot, "PetAttack");        
        mPetDefense = UIAttr.CreateUIAttr(this.mRoot, "PetDefense");
        mPetLife = UIAttr.CreateUIAttr(this.mRoot, "PetLife");                   
        mPetCrit = UIAttr.CreateUIAttr(this.mRoot, "PetCrit");                    
        mPetDodge = UIAttr.CreateUIAttr(this.mRoot, "PetDodge");      
        mPetRate = UIAttr.CreateUIAttr(this.mRoot, "PetRate");           
    }

    public void UpdateAttr(Q_PetUpLevelBean info)
    {
        mPetAttack.mAttrValue.text = info.attack.ToString();
        mPetDefense.mAttrValue.text = info.defense.ToString();
        mPetLife.mAttrValue.text = info.maxhp.ToString();
        mPetCrit.mAttrValue.text = info.crit.ToString();
        mPetDodge.mAttrValue.text = info.dodge.ToString();
        mPetRate.mAttrValue.text = info.rate.ToString();
    }

    public static UIPetAttributeView CreatePetAttributeView(GameObject p)
    {
        GameObject attrGo = DisplayUtil.GetChild(p, "PetAttributeView");
        if (attrGo == null) return null;
        UIPetAttributeView attr = new UIPetAttributeView(attrGo);
        return attr;
    }
}
2、断点找错

后来发现

UIPetAttributeView attr = new UIPetAttributeView(attrGo);
        return attr;
实例化的对象attr为null,反复检查得出问题出在
public class UIPetAttributeView  : MonoBehaviour
3、总结

在unity 中继承自MonoBehaviour的类是不可以实例化的,需要调用AddComponent方法



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值