2017-10-05 356 views
0

我有2個腳本:控制檯和測試。我想從測試腳本調用「appendLogLine」函數,但無法使其工作。Unity從其他腳本調用類功能

Console.cs:

public class ConsoleController 
{ 

    public void appendLogLine(string line) 
    { 
     if (line == "Unable to process command ''") 
      return; 

     Debug.Log(line); 

     if (scrollback.Count >= ConsoleController.scrollbackSize) 
     { 
      scrollback.Dequeue(); 
     } 
     scrollback.Enqueue(line); 

     log = scrollback.ToArray(); 
     if (logChanged != null) 
     { 
      logChanged(log); 
     } 
    } 
} 

test.cs中:

public GameObject ConsoleObject; 

public void CallLog() 
{ 

    ConsoleObject.GetComponent<ConsoleController>.appendLogLine ("Test123"); 
} 

我得到的錯誤與: 「錯誤CS0119:表達式表示method group', where a變量 'value' or型' 預期」

+1

'ConsoleObject.GetComponent ()appendLogLine( 「Test123」);' – zwcloud

回答

4

爲了使用GetComponent,您正在執行的腳本GetComponent必須從MonoBehaviour繼承。這裏不是這種情況。

public class ConsoleController {}應該public class ConsoleController : MonoBehaviour {}

現在,你在ConsoleController腳本中使用GetComponent。請注意,您也忘記了「()」。由於GetComponent是一個函數,因此您必須包含該值。

它應該是這樣的:

ConsoleObject.GetComponent<ConsoleController>().appendLogLine("Test123");