我有以下方法:
protected T AttachComponent<T>(){
T gsComponent = gameObject.GetComponent<T>();
if(gsComponent == null){
gsComponent = gameObject.AddComponent<T>();
}
return gsComponent;
}
在AddComponent
行我收到以下错误:
类型“ T”不能用作通用类型或方法“ GameObject.AddComponent()”中的类型参数“ T”。没有从“ T”到“ UnityEngine.Component”的装箱转换或类型参数转换。
我不确定该如何解决此错误,为什么我不能这样做?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句