我在Unity中为VR制作射击游戏,但我无法拍摄物体。每当我指向对象时,它都会抛出此错误。我尝试了其他相同错误的帖子,但他们没有回答我的问题。
ERROR- ArgumentException:要实例化的Object为null。 UnityEngine.Object.CheckNullArgument(System.Object arg,System.String message)(at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239) UnityEngine.Object.Instantiate(UnityEngine.Object original)(at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:176) playerScript + c__Iterator0.MoveNext()(在Assets / Scripts / playerScript.cs:30) UnityEngine.SetupCoroutine.InvokeMoveNext(IEnumerator枚举器,IntPtr returnValueAddress)(在/Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(字符串) playerScript:Update()(在Assets / Scripts / playerScript.cs:61)
这是我使用的代码 -
使用UnityEngine; 使用System.Collections;
public class playerScript:MonoBehaviour {
//declare GameObjects and create isShooting boolean.
private GameObject gun;
private GameObject spawnPoint;
private bool isShooting;
// Use this for initialization
void Start () {
//only needed for IOS
Application.targetFrameRate = 60;
//create references to gun and bullet spawnPoint objects
gun = gameObject.transform.GetChild (0).gameObject;
spawnPoint = gun.transform.GetChild (0).gameObject;
//set isShooting bool to default of false
isShooting = false;
}
//Shoot function is IEnumerator so we can delay for seconds
IEnumerator Shoot() {
//set is shooting to true so we can't shoot continuosly
isShooting = true;
//instantiate the bullet
GameObject bullet = Instantiate(Resources.Load("bullet", typeof(GameObject))) as GameObject;
//Get the bullet's rigid body component and set its position and rotation equal to that of the spawnPoint
Rigidbody rb = bullet.GetComponent<Rigidbody>();
bullet.transform.rotation = spawnPoint.transform.rotation;
bullet.transform.position = spawnPoint.transform.position;
//add force to the bullet in the direction of the spawnPoint's forward vector
rb.AddForce(spawnPoint.transform.forward * 500f);
//play the gun shot sound and gun animation
GetComponent<AudioSource>().Play ();
gun.GetComponent<Animation>().Play ();
//destroy the bullet after 1 second
Destroy (bullet, 1);
//wait for 1 second and set isShooting to false so we can shoot again
yield return new WaitForSeconds (1f);
isShooting = false;
}
// Update is called once per frame
void Update () {
//declare a new RayCastHit
RaycastHit hit;
//draw the ray for debuging purposes (will only show up in scene view)
Debug.DrawRay(spawnPoint.transform.position, spawnPoint.transform.forward, Color.green);
//cast a ray from the spawnpoint in the direction of its forward vector
if (Physics.Raycast(spawnPoint.transform.position, spawnPoint.transform.forward, out hit, 100)){
//if the raycast hits any game object where its name contains "zombie" and we aren't already shooting we will start the shooting coroutine
if (hit.collider.name.Contains("Shooting Object")) {
if (!isShooting) {
StartCoroutine ("Shoot");
}
}
}
}
}
答案 0 :(得分:1)
问题在于这一行
GameObject bullet = Instantiate(Resources.Load("bullet", typeof(GameObject))) as GameObject;
它无法找到资源&#34; bullet。&#34;确保您已将其部署到right folder。