Raycast让gameobject被击中以在gameobject上运行脚本和功能

时间:2015-05-03 23:11:19

标签: c# unity3d reference raycasting

我正在为我的大学项目让多个敌人在我的游戏中工作。我有一个带有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);
    }
}

}

任何帮助,试图弄清楚为什么它没有得到参考/设置它们将非常感谢,谢谢。

1 个答案:

答案 0 :(得分:1)

enHit可能为null。重温所有

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

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

您的脚本中大约有4个。你想让EnemyHealth Script附加到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")

这可能无法解决您遇到的当前错误,但这是导致该错误的问题之一。