我正在爲我的大學項目爭取多個敵人在我的遊戲中工作。我有一名玩家使用ShootGun腳本來對槍進行光線投射,並檢測對象是否是敵方對手(標有Enemy_Head,_Torso和_Limb)之一。如果是,它將獲得敵方命中的gameobject和該gameobject上的enemyHealth腳本組件,然後將在該腳本上調用公共函數。當前,當ShootGun腳本嘗試獲取組件/腳本時,它會顯示以下錯誤:Raycast可以讓gameobject在gameobject上運行腳本和函數
NullReferenceException:未將對象引用設置爲對象的實例 ShootGun.gunFire()(在Assets/Scripts/Gun/ShootGun的.cs:107)
更新的誤差CODE:
的NullReferenceException:未設置爲一個對象 ShootGun.gunFire()的一個實例(在資產/腳本/噴槍/ ShootGun.cs對象引用:113 )
Line 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行。你可以在該行上發佈代碼嗎? – Programmer
107是這一行: \t \t \t \t \t \t enhit。enemyShotTorso(); 在Enemy_Torso下檢查是否聲明 – ixTec