[英]Unity Call a class function from other script
我有2個腳本:控制台和測試。 我想從測試腳本中調用“ appendLogLine”函數,但無法使其正常工作。
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
類型”
為了使用GetComponent
,您正在執行GetComponent
的腳本必須繼承自MonoBehaviour
。 這里不是這種情況。
public class ConsoleController {}
應該是public class ConsoleController : MonoBehaviour {}
現在,您可以在ConsoleController
腳本上使用GetComponent
。 請注意,您也忘記了“()”。 由於GetComponent
是一個函數,因此必須包括該函數。
應該是這樣的:
ConsoleObject.GetComponent<ConsoleController>().appendLogLine("Test123");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.