MonoBehaviour 类的问题
2015-07-03 11:11
267 查看
1.错误代码
今天写了一串代码,不知道那里错了
后来发现
在unity 中继承自MonoBehaviour的类是不可以实例化的,需要调用AddComponent方法
今天写了一串代码,不知道那里错了
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 : MonoBehaviour3、总结
在unity 中继承自MonoBehaviour的类是不可以实例化的,需要调用AddComponent方法
相关文章推荐
- 设计模式读书笔记:Builder(建造者)
- JS 获取节点属性值
- DJANGO配置静态资源
- 这才是真正的裸眼3D!超级震撼!!
- DataTable和DataSet有什么区别
- 三极管工作原理分析
- jQuery简单邮箱验证
- gitlab 一键安装
- oracle银行卡卡号计算函数
- 性能测试监控:Linux/unix监控工具nmon
- mysql 数据库备份ubuntu
- iOS开发网络篇之Base64编解码
- AJAX了解与用法
- Ubuntu12.04安装vnc Server
- 索尼单反相机跑焦的调整 A700 7D 5D
- 迭代器报“vector iterators incompatible”错的几种可能
- Android数据存储之Preference
- Excel表格复制到word中
- Spring IOC-ApplicationContext的继承关系
- java 参数传递