您的位置:首页 > 其它

MonoBehaviour 类的问题

2015-07-03 11:11 267 查看
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方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: