无法在Unity游戏中射击物体. Error-ArgumentException:您要实例化的对象为null

问题描述:

我正在Unity中为VR制作射击游戏,但无法射击物体.每当我指向对象时,它都会引发此错误.我尝试了其他具有相同错误的帖子,但它们没有解决我的问题.

I'm making a Shooting game for VR in Unity and I'm unable to shoot the object. Everytime I point the object, it throws this error. I tried other posts with same error but they does not answer my problem.

错误- ArgumentException:您要实例化的对象为null. UnityEngine.Object.CheckNullArgument(System.Object arg,System.String消息)(位于/Users/builduser/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239) UnityEngine.Object.Instantiate(UnityEngine.Object原始)(位于/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(String) playerScript:Update()(在Assets/Scripts/playerScript.cs:61中)

ERROR- ArgumentException: The Object you want to instantiate is 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 () (at Assets/Scripts/playerScript.cs:30) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(String) playerScript:Update() (at Assets/Scripts/playerScript.cs:61)

我附上一张图片,以更好地了解场景.黄色的立方体是射击对象.

I'm attaching an image for better understanding of the scene. The yellow cubes are the Shooting Object.

这是我正在使用的代码-

Here's the code I'm using-

使用UnityEngine; 使用System.Collections;

using UnityEngine; using System.Collections;

公共类playerScript:MonoBehaviour {

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");
            }

        }

    }

}

}

问题是这一行

GameObject bullet = Instantiate(Resources.Load("bullet", typeof(GameObject))) as GameObject;

找不到资源项目符号".确保已将其部署到

It can't find the resource "bullet." Make sure you've deployed it into the right folder.