2015-05-03 40 views
0

我正在爲我的大學項目爭取多個敵人在我的遊戲中工作。我有一名玩家使用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); 
    } 
} 

}

,試圖弄清楚爲什麼它不是任何幫助獲得參考/設置他們將不勝感激,謝謝。

+0

運行時錯誤發生在ShootGun.cs的第107行。你可以在該行上發佈代碼嗎? – Programmer

+0

107是這一行: \t \t \t \t \t \t enhit。enemyShotTorso(); 在Enemy_Torso下檢查是否聲明 – ixTec

回答

1

enhit很可能爲空。 Repace所有

hit.transform.gameObject.GetComponent<EnemyHealth>(); 

hit.collider.gameObject.GetComponent<EnemyHealth>(); 

你有其中約4腳本。您希望將EnemyHealth腳本附加到Ray通過對撞機擊中的對象上。

編輯:

你也需要改變

hit.transform.CompareTag("Enemy_Head") 
hit.transform.CompareTag("Enemy_Torso") 
hit.transform.CompareTag("Enemy_Limb") 

hit.collider.gameObject.CompareTag("Enemy_Head") 
hit.collider.gameObject.CompareTag("Enemy_Torso") 
hit.collider.gameObject.CompareTag("Enemy_Limb") 

這也許不能解決你所得到的電流誤差,但它的問題之一正在導致該錯誤。

+0

我試過了,但我需要調用該腳本的特定函數來處理正確的傷害量(取決於身體部位的命中)。 enHint/should /被設置爲hit.collider.gameObject.GetComponent ()它似乎沒有檢測到它作爲參考? – ixTec

+0

取代enHit = hit.transform.gameObject.GetComponent (); 與 enHit = hit.collider.gameObject.GetComponent (); 然後發佈你有錯誤。在你做之前,編輯你的問題並用我所說的更新你的代碼。 – Programmer

+0

更新的代碼。 錯誤:NullReferenceException:未將對象引用設置爲對象的實例ShootGun.gunFire()(在Assets/Scripts/Gun/ShootGun.cs:113處) Line 113 = enHit.enemyShotTorso(); 對於enHit嘗試從enemyHealth腳本調用某個函數的每一行,都會顯示該錯誤。 – ixTec