【问题标题】:Unity : Delegate `System.Action' does not take `1' argumentsUnity:委托“System.Action”不接受“1”参数
【发布时间】:2017-07-18 02:19:22
【问题描述】:

我有以下代码:

socket.On ("chat", (data) => {
                string str = data.ToString ();

                ChatData chat = JsonConvert.DeserializeObject<ChatData> (str);
                string strChatLog = "user#" + chat.id + ": " + chat.msg;

                int pos = userArr.IndexOf (chat.id);
                if (pos > -1) {
                    var InsObj = Instantiate (player, StringToVector3 (chat.msg), Quaternion.identity);
                    InsObj.name = chat.id;
                    userArr [userArr.Length + 1] = chat.id;
                }

                // Access to Unity UI is not allowed in a background thread, so let's put into a shared variable
                lock (chatLog) {
                    chatLog.Add (strChatLog);
                }
            });

当我尝试运行它时,我得到了这个错误:

Assets/SocketIO/SocketIOScript.cs(59,23): error CS1593: Delegate `System.Action' does not take `1' arguments

但是当我删除这部分时:

int pos = userArr.IndexOf (chat.id);
                if (pos > -1) {
                    var InsObj = Instantiate (player, StringToVector3 (chat.msg), Quaternion.identity);
                    InsObj.name = chat.id;
                    userArr [userArr.Length + 1] = chat.id;
                }

它工作得很好。

我想知道为什么会发生这种情况,我已经尝试了很长时间。我找到了this 的帖子,但这对我没有多大帮助,因为我不认为我可以指定data。

编辑:

StringToVector3 包含以下代码(但是即使我摆脱了StringToVector3 我仍然会收到错误消息):

public static Vector3 StringToVector3 (string sVector)
    {
        // Remove the parentheses
        if (sVector.StartsWith ("(") && sVector.EndsWith (")")) {
            sVector = sVector.Substring (1, sVector.Length - 2);
        }

        // split the items
        string[] sArray = sVector.Split (',');

        // store as a Vector3
        Vector3 result = new Vector3 (
                             float.Parse (sArray [0]),
                             float.Parse (sArray [1]),
                             float.Parse (sArray [2]));

        return result;
    }

【问题讨论】:

  • 什么是StringToVector3 ?看起来您正在调用返回类型为 void 或 Action 的函数
  • 我已经添加了StringToVector3,但是如果我排除它,我仍然会遇到同样的错误。感谢您的帮助!
  • StringToVector3 已经返回 Vector3 - 你真的需要将它传递给构造函数吗?你能改写吗:Instantiate (player, StringToVector3 (chat.msg), Quaternion.identity);
  • 感谢您注意到这一点。我进行了编辑,但是使用您提供的代码仍然会出现相同的错误,如果我完全删除 StringToVectory3,我什至会收到该错误。

标签: c# unity3d


【解决方案1】:

问题是我需要使用System.Array.IndexOf 而不是userArr.IndexOf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2013-04-22
    相关资源
    最近更新 更多