首页 文章

无法在Unity游戏中拍摄对象 . Error-ArgumentException:要实例化的Object为null

提问于
浏览
1

我正在为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) (在/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 )

我附上一张图片以便更好地了解场景 . 黄色立方体是射击物体 .
enter image description here

这是我正在使用的代码 -

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

        }

    }

}

}

1 回答

  • 1

    问题是这一行

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

    它可以't find the resource 645520 Make sure you'已将其部署到right folder中 .

相关问题