在我的VR应用程序中,我想在用户面前显示键盘预制件。这是我的脚本附加到键盘预制件:
public class Keyboard : MonoBehaviour {
public GameObject keyboardPrefab; //this gets assigned in the inspector
public void showKeyboard()
{
Vector3 spawnPosition = GameObject.Find("[CameraRig]").transform.position;
spawnPosition.z += 10;
Instantiate(keyboardPrefab, spawnPosition, Quaternion.identity);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
在另一个剧本中:
Keyboard kb = gameObject.AddComponent<Keyboard>();
kb.showKeyboard()
但是,我总是得到相同的错误消息:
是的,有人能帮帮我吗?我做错了什么?ArgumentException:要实例化的Object为null。 UnityEngine.Object.CheckNullArgument(System.Object arg,System.String 消息)(at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:353) UnityEngine.Object.Instantiate(UnityEngine.Object原创,Vector3 位置,四元数旋转)(at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:203) UnityEngine.Object.Instantiate [GameObject](UnityEngine.GameObject 原始,Vector3位置,四元数旋转)(at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:257) Keyboard.showKeyboard()(在Assets / Scripts / Keyboard.cs:18) InteractionScript.Update()(at 资产/脚本/ InteractionScript.cs:151)