如何在任何其他脚本的任何其他函数中调用下面的函数?
我试图在这样的其他脚本中调用这个函数
grab.StartGrab();
但它不起作用
public VRTK_ChildOfControllerGrabAttach other;
public void move()
{
other.GetComponent<VRTK_ChildOfControllerGrabAttach>().StartGrab(//parameters here but how);
}
这是函数
public override bool StartGrab(GameObject grabbingObject, GameObject givenGrabbedObject, Rigidbody givenControllerAttachPoint)
{
if (base.StartGrab(grabbingObject, givenGrabbedObject, givenControllerAttachPoint))
{
SnapObjectToGrabToController(givenGrabbedObject);
grabbedObjectScript.isKinematic = true;
return true;
}
return false;
}
您应该使用GetComponent<"ScriptName">.StartGrab()
, 其中ScriptName
是包含StartGrab()
.
请注意,因为您的方法有 3 个参数,所以当您调用它时,您应该传递它们,否则您会遇到编译器错误。
例如:
GetComponent<"ScriptName">.StartGrab(objectToGrab, grabbedObject, objectController);
您现在正确地调用了该方法。需要注意的是objectToGrab
,grabbedObject
并objectController
必须在您所呼叫的方法脚本中声明,并且必须是类型等同的参数StartGrab()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句