错误说函数在实际需要 none 时需要 1 个参数
Error says function requires 1 parameter when it actually requires none
在 Unity 场景中,我希望两个对象通过 SendMessage 进行通信,这样我就不必在运行时执行 GetComponent。
这是 SendMessage 调用:
hit.transform.gameObject.SendMessage("OnSelect", SendMessageOptions.RequireReceiver);
在另一个对象(称为 Button)上,我有一个具有此功能的脚本:
public void OnSelect()
{
dialogueManager.DisplayDialogue(this);
}
当我玩游戏时,OnSelect 函数运行没有任何问题,并完成了它需要做的一切,但控制台给我错误 'Failed to call function OnSelect of class Button; Calling function with no parameters but the function requires 1.'
为什么说函数需要 1 个参数,而实际上需要 none?我错过了什么吗?
两件事:
Unity 的按钮 class 确实已经包含一个名为“OnSelect”的函数,它接受一个参数。您可以将函数重命名为不同的名称 https://docs.unity3d.com/530/Documentation/ScriptReference/UI.Selectable.OnSelect.html
SendMessageOptions 应该是第三个参数而不是第二个。所以语法应该是这样的:
hit.transform.gameObject.SendMessage("OnCustomHit", null, SendMessageOptions.RequireReceiver);
https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html
在 Unity 场景中,我希望两个对象通过 SendMessage 进行通信,这样我就不必在运行时执行 GetComponent。
这是 SendMessage 调用:
hit.transform.gameObject.SendMessage("OnSelect", SendMessageOptions.RequireReceiver);
在另一个对象(称为 Button)上,我有一个具有此功能的脚本:
public void OnSelect()
{
dialogueManager.DisplayDialogue(this);
}
当我玩游戏时,OnSelect 函数运行没有任何问题,并完成了它需要做的一切,但控制台给我错误 'Failed to call function OnSelect of class Button; Calling function with no parameters but the function requires 1.'
为什么说函数需要 1 个参数,而实际上需要 none?我错过了什么吗?
两件事:
Unity 的按钮 class 确实已经包含一个名为“OnSelect”的函数,它接受一个参数。您可以将函数重命名为不同的名称 https://docs.unity3d.com/530/Documentation/ScriptReference/UI.Selectable.OnSelect.html
SendMessageOptions 应该是第三个参数而不是第二个。所以语法应该是这样的:
hit.transform.gameObject.SendMessage("OnCustomHit", null, SendMessageOptions.RequireReceiver);
https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html