【问题标题】:Unity Call a class function from other scriptUnity 从其他脚本调用类函数
【发布时间】:2017-10-05 14:15:11
【问题描述】:

我有 2 个脚本:控制台和测试。我想从测试脚本中调用“appendLogLine”函数,但无法正常工作。

控制台.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 avariable',value' ortype' 是预期的”

【问题讨论】:

  • ConsoleObject.GetComponent&lt;ConsoleController&gt;().appendLogLine ("Test123");

标签: c# function class unity3d


【解决方案1】:

为了使用GetComponent,您正在执行GetComponent 的脚本必须继承自MonoBehaviour。这不是这里的情况。

public class ConsoleController {} 应该是public class ConsoleController : MonoBehaviour {}

现在,您在 ConsoleController 脚​​本上使用 GetComponent。请注意,您也忘记了“()”。您必须包含它,因为 GetComponent 是一个函数。

应该是这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2013-07-20
    • 1970-01-01
    • 2013-11-12
    • 2019-06-02
    • 1970-01-01
    相关资源
    最近更新 更多