You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
MonoBehaviour:.ctor()
cs:.ctor()
什么情况
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all这个出现好几次
using UnityEngine;
using System.Collections;
public class csmouspo : MonoBehaviour {
cs cs1=new cs();
void Start(){
int i=this.cs1.cshs();
print ("r:"+i+"\n");
}
using UnityEngine;
using System.Collections;
public class cs : MonoBehaviour {
public int cshs() {
return 1; }
}
这个不是C#的错误,只能翻译下:
你正在试图用new关键字创建一个MonoBehaviour,这是不允许的。MonoBehaviours只能通过AddComponent()被添加,或者,你的脚本可以从ScriptableObject继承或者根本没有基类。
cs cs1=new cs(); 改成cs cs1=cs.AddComponent();
改成cs cs1=MonoBehaviour.AddComponent();呢,只是根据错误提示来说应该是这样解决。
cs cs1=this
cs 的类完全没必要继承Mono的,继承没必要多用,能不能则不添加,这样才是高效的代码,(~ o ~)Y