【发布时间】:2023-04-02 16:31:01
【问题描述】:
我正在让多个敌人在我的游戏中为我的大学项目工作。我有一个带有 ShootGun 脚本的玩家,该脚本对枪进行射线投射并检测被击中的物体是否是敌对撞机之一(标记为 Enemy_Head、_Torso 和 _Limb)。如果是,它将获取被击中的敌人的游戏对象和该游戏对象上的enemyHealth 脚本组件,然后将调用该脚本上的公共函数。目前,当 ShootGun 脚本尝试获取组件/脚本时,它会显示以下错误:
NullReferenceException:对象引用未设置为对象的实例 ShootGun.gunFire () (在 Assets/Scripts/Gun/ShootGun.cs:107)
更新的错误代码:
NullReferenceException:对象引用未设置为对象的实例 ShootGun.gunFire () (在 Assets/Scripts/Gun/ShootGun.cs:113)
第 113 行 = enHit.enemyShotTorso();
enHit 尝试从enemyHealth 脚本调用函数的每一行都会出现错误。
以下是我的 ShootGun 脚本中的光线投射:
// Raycasting for bullet projection against obstacles within the world (WIP)
float gunRayDistance = 50f;
Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
// Name what for the raycast collides with (used to reference the target point)
RaycastHit hit;
// The actual raycast
if(Physics.Raycast(ray, out hit, gunRayDistance, 1 << 9) || Physics.Raycast(ray, out hit, gunRayDistance, 1 << 8)) {
Debug.Log("Bullet Hit");
EnemyHealth enHit = hit.collider.gameObject.GetComponent<EnemyHealth>();
// Checking if the raycast (bullet) collided with objects tagged with "Enemy_Head".
if (hit.collider.gameObject.CompareTag("Enemy_Head")) {
Debug.Log ("Headshot!");
//hitPoint = hit.collider.gameObject;
enHit = hit.collider.gameObject.GetComponent<EnemyHealth>();
enHit.enemyShotHead();
}
// Checking if the raycast (bullet) collided with objects tagged with "Enemy_Torso".
if (hit.collider.gameObject.CompareTag("Enemy_Torso")) {
Debug.Log ("Body-shot!");
//hitPoint = hit.collider.gameObject;
enHit = hit.collider.gameObject.GetComponent<EnemyHealth>();
enHit.enemyShotTorso();
}
// Checking if the raycast (bullet) collided with objects tagged with "Enemy_Limb".
if (hit.collider.gameObject.CompareTag("Enemy_Limb")) {
Debug.Log ("Limb-shot!");
enHit = hit.collider.gameObject.GetComponent<EnemyHealth>();
enHit.enemyShotLimb();
}
// The point of contact with the model is given by the hit.point (to not cause z-fighting issues with layering)
Vector3 bulletHolePosition = hit.point + hit.normal * 0.01f;
// Rotation to match where it hits (between the quad vector forward axis and the hit normal)
Quaternion bulletHoleRotation = Quaternion.FromToRotation(-Vector3.forward, hit.normal);
GameObject hole = (GameObject)GameObject.Instantiate(bulletHolePrefab, bulletHolePosition, bulletHoleRotation);
// Destroy the instantiated gameobject of the bulletHole after a delay of 5 seconds.
Destroy (hole, 5.0f);
}
}
以下是我的 EnemyHealth 脚本:
public class EnemyHealth : MonoBehaviour {
public float enemyHealth = 100.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
enemyDeath ();
}
public void enemyShotHead() {
enemyHealth -= 60f;
Debug.Log (enemyHealth);
}
public void enemyShotTorso() {
enemyHealth -= 40f;
Debug.Log (enemyHealth);
}
public void enemyShotLimb() {
enemyHealth -= 20f;
Debug.Log (enemyHealth);
}
void enemyDeath() {
if (enemyHealth <= 0.0f) {
Debug.Log ("Enemy Killed");
gameObject.SetActive(false);
}
}
}
任何帮助试图找出为什么它没有得到参考/设置它们将不胜感激,谢谢。
【问题讨论】:
-
ShootGun.cs 的第 107 行出现运行时错误。你能把代码贴在那行吗?
-
107 是这一行:enHit.enemyShotTorso();在 Enemy_Torso 下检查 if 语句
标签: c# unity3d reference raycasting