【问题标题】:Warning: You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed [duplicate]警告:您正在尝试使用“new”关键字创建 MonoBehaviour。这是不允许的[重复]
【发布时间】:2020-07-24 16:38:00
【问题描述】:

对于一个学校项目,我在 Unity 中构建了一个 API。这样做时,出现了以下警告:

您正在尝试使用“new”关键字创建 MonoBehaviour。这是不允许的。 MonoBehaviours 只能使用 AddComponent() 添加。或者,您的脚本可以继承自 ScriptableObject 或根本不继承基类 UnityEngine.MonoBehaviour:.ctor()

我没有游戏对象。它是一个应该提供纯界面的脚本。下面是我的代码。有人可以帮我吗?

public class ApiPost : MonoBehaviour
{
    public static bool PostDataSchachtGui(string url,string SchachtNrGui)
    {
        bool isfinish = false;

        var instance = new ApiPost();
        instance.StartCoroutine(Post(url, SchachtNrGui));

        IEnumerator Post(string uri, string schacht_nr)
        {
            WWWForm form = new WWWForm();
            form.AddField("SchachtNr1", schacht_nr);

            UnityWebRequest www = UnityWebRequest.Post(uri, form);
            www.chunkedTransfer = false;

            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
                isfinish = false;
            } 
            else
            {
                isfinish = true;
            }
        }
        return isfinish;
    }
}
{
    // Start is called before the first frame update
    public void abfragen(string button)
    {
        if (button == "start")
        {
            StartBtn.postdata();
        }
        else
        {

        }
    }
}

public static void postdata()
{
    //Suche Inputfeld
    GameObject Schacht_field = GameObject.Find("Schacht_field");
    input Schacht = Schacht_field.GetComponent<input>();
    SchachtNrGui = Schacht.inputtext;
    //Setze Globale Daten
    StaticData.SchachtNrDataShow = SchachtNrGui;
    //Poste Daten
    var check = ApiPost.PostDataSchachtGui("http://localhost/prototype1/setumgevar.php", SchachtNrGui);
    if (check == true)
    {
        //ApiGet.GetAllData("http://localhost/prototype1/getdata.php");
    }
}

【问题讨论】:

    标签: c# api unity3d


    【解决方案1】:

    您的问题在于var instance = new ApiPost(); 的行。

    您的类 ApiPost 继承自 Monobehaviour 并且与错误状态一样,您不能使用 new 关键字创建 MonoBehaviour,但您可以通过 AddComponent() 方法添加 MonoBehaviours。

    你有两个主要的行动过程

    1. 从第一行的Monobehaviour 中删除继承。而不是public class ApiPost : MonoBehaviour,只需使用public class ApiPost
    2. 重构代码的基础,以不同的方式创建 ApiPost 实例。您可以Instantiate 它附加到的对象、GetComponent 对象或其他方法。例如,您可以通过将ApiPost 附加到ApiGameObject 来访问,然后调用:

      var instance = ApiGameObject.GetComponent<ApiPost>();
      

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      你能更具体地回答这个问题吗?这里的枚举是什么?

      如果您在谈论ApiPost,那么是的,您不能像那样实例化 MonoBehaviour。您必须将该脚本附加到游戏对象,然后在脚本中引用该游戏对象。然后使用该游戏对象,您可以使用 GetComponent() 访问您的脚本。

      所以是这样的:

      //write this in your class
      public GameObject myGameObject;
      
      //write this in your function:
      var instance = myGameObject.GetComponent<ApiPost>();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-17
        相关资源
        最近更新 更多