2017-07-18 51 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