2017-07-18 48 views
-1

我有以下代码:统一:委托`System.Action”不采取'1' 参数

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; 
    } 
+0

什么是StringToVector3?看起来你正在调用一个带'void'返回类型的函数,或者一个'Action' – Rob

+0

我已经添加了'StringToVector3',但是如果我排除它,我仍然会得到相同的错误。感谢您的帮助! – pudility

+0

'StringToVector3'已经返回一个'Vector3' - 你真的需要将它传递给构造函数吗?你可以写:'Instantiate(player,StringToVector3(chat.msg),Quaternion.identity);' – Rob

回答

0

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