无法在 Unity 游戏中射击物体。 Error-ArgumentException:您要实例化的对象为空
Unable to shoot an object in Unity game. Error-ArgumentException: The Object you want to instantiate is null
我正在 Unity 中制作 VR 射击游戏,但我无法射击物体。每次我指向该对象时,它都会抛出此错误。我尝试了其他有同样错误的帖子,但他们没有回答我的问题。
错误-
ArgumentException:您要实例化的对象为空。
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 返回值地址)(位于 /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:启动协程(字符串)
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");
}
}
}
}
}
问题出在这一行
GameObject bullet = Instantiate(Resources.Load("bullet", typeof(GameObject))) as GameObject;
它找不到资源"bullet."确保你已经将它部署到right folder。
我正在 Unity 中制作 VR 射击游戏,但我无法射击物体。每次我指向该对象时,它都会抛出此错误。我尝试了其他有同样错误的帖子,但他们没有回答我的问题。
错误- ArgumentException:您要实例化的对象为空。 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 返回值地址)(位于 /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:启动协程(字符串) 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");
}
}
}
}
}
问题出在这一行
GameObject bullet = Instantiate(Resources.Load("bullet", typeof(GameObject))) as GameObject;
它找不到资源"bullet."确保你已经将它部署到right folder。