Unity 2D(空参考例外)

时间:2017-03-27 08:29:22

标签: c# unity3d

我正在制作一个基于超级板条盒设计的游戏:玩家与盒子碰撞并选择随机武器。
问题是如果我用另一支枪再次射击时霰弹枪子弹是活动的,我得到了例外(我很明显会让子弹消失,但即使子弹消失也会出现问题)。

我需要非常快速地按下键才能发生这种情况所以我认为只有在霰弹枪子弹激活时才会发生异常。

例外情况:

  • 玩家拿起霰弹枪
  • 玩家射击子弹(子弹不会因测试目的而消失)
  • 玩家拿起另一支枪
  • 当子弹仍处于活动状态时,玩家再次按下控制以发射子弹 =>例外

发生异常的行是此Transform cameraTransform = player.transform;的第一个实例。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class FireGun : MonoBehaviour
{

     //used to hold the guns user is currently holding
     public List Guns = new List();
     //to acceess pickupbx
     public PickupBx pbx;
     //holds the current name weapon user is holding
     private string weaponFireType;
     //this will get the bullet prefab
     public GameObject BulletPrefab;
     public GameObject shotGunBulletPrefab;
     //bullet position on player
     public GameObject bulletPosition;
     public GameObject shotgunBulletPosition;
     //the player
     public GameObject player;
     //this is the bullet that will be instantiated in scene (rpg/machine      gun)
     public GameObject bulletInScene;
     //this is for the shotgun bullet that will be instantiated
     public GameObject ShotgunBulletInScene;
     //used to make shooty shoot once every few seconds
     private bool shootShottyOnce = false;

     void Start()
     {
          pbx = GetComponent();
     }

     // Update is called once per frame
     void Update()
     {
          if (GetComponent().flipX == true)
          {
               bulletPosition.transform.position = new Vector2(-92, 0);
          }
          else if (GetComponent().flipX == false)
          {
               bulletPosition.transform.position = new Vector2(92, 0);
          }
          AddObjectToList(pbx.guns, Guns);
          checkWeaponType();
     }


     //this method will loop through the gameobjects in pbxguns list (first parameter is the guns list in the pickupBx script), (myList is the list in this script)_
     void AddObjectToList(List pbxGunsList, List myList)
     {
          //loop through all gaeobjects in the pbxGunsList list
          foreach (GameObject go in pbxGunsList)
          {
               //if this list in this script doesn't contain the gameObjects      from the other script
               if (!myList.Contains(go))
               {
                    //clear the list to make sure we don't get duplicates and have weapons in one 1 element
                    myList.Clear();
                    //add the gun to our list
                    myList.Add(go);
               }
          }
     }

     //check weapon the player is holding
     void checkWeaponType()
     {
          //loop through the guns and set the string to weapon player is currently holding
          for (int i = 0; i < Guns.Count; i++)
          {
               if (Guns[i].name == "weapons_0(Clone)")
               {
                    weaponFireType = "MachineGun";
               }
               else if (Guns[i].name == "weapons_1(Clone)")
               {
                    weaponFireType = "Shotgun";
               }
               else if (Guns[i].name == "weapons_2(Clone)")
               {
                    weaponFireType = "RPG";
               }
          }
          //check if user pressed key and check that guns is not empty ( reason shotgun is inputed here is because the shotgun will shoot differently to the other guns
          if (Input.GetKeyDown(KeyCode.LeftControl) && Guns.Count != 0 && weaponFireType != "Shotgun")
          {
               //make a new gameObject which will hold the bullet in the scene it will instantiate a bullet at the position of the bulletPosition empty gameObject.
               bulletInScene = (GameObject)Instantiate(BulletPrefab, bulletPosition.transform.position, Quaternion.identity);
               //the transform of the player
               Transform cameraTransform = player.transform;
               //Makes the player the parent of the GameObject currentGun
               bulletInScene.transform.SetParent(cameraTransform, false);
          }
          if (Input.GetKeyDown(KeyCode.LeftControl) && weaponFireType == "Shotgun")
          {
               ShotgunBulletInScene = (GameObject)Instantiate(shotGunBulletPrefab, shotgunBulletPosition.transform.position, Quaternion.identity);
               //the transform of the player
               Transform cameraTransform = player.transform;
               //Makes the player the parent of the GameObject currentGun
               ShotgunBulletInScene.transform.SetParent(cameraTransform, false);
          }
          foreach (Transform t in transform)
          {
               if (bulletInScene != null)
               {
                    if (t.name.Contains("Bullet1(Clone)"))
                    {
                         transform.Find("Bullet1(Clone)").transform.parent = null;
                         if (GetComponent().flipX == true && weaponFireType == "MachineGun")
                              bulletInScene.GetComponent().AddForce(Vector2.left * 7500);
                         if (GetComponent().flipX == false && weaponFireType == "MachineGun")
                              bulletInScene.GetComponent().AddForce(Vector2.right * 7500);
                         if (GetComponent().flipX == true && weaponFireType == "RPG")
                              bulletInScene.GetComponent().AddForce(Vector2.left * 4500);
                         if (GetComponent().flipX == false && weaponFireType == "RPG")
                              bulletInScene.GetComponent().AddForce(Vector2.right * 4500);
                    }
               }
          }
          foreach (Transform t in transform)
          {
               if(ShotgunBulletInScene != null)
               {
                    if (t.name.Contains("ShotgunBullet1(Clone)"))
                    {
                         Debug.Log("shotty test");
                         if (shootShottyOnce == false)
                         {
                              StartCoroutine(DestroyShottyBullet());
                         }
                    }
               }
          }
     }

     IEnumerator DestroyShottyBullet()
     {
          shootShottyOnce = true;
          yield return new WaitForSeconds(0.5f);
          Destroy(ShotgunBulletInScene);
          shootShottyOnce = false;
     }
}

1 个答案:

答案 0 :(得分:0)

该错误表明未设置player。从代码判断我假设你有一个预制件,你在检查器中设置它,或者另一个脚本应该设置它。

当猎枪子弹处于活动状态并且您忘记在该预制件上分配player时,也许您出于任何原因使用不同的预制件。

或者应该在player MonoBehaviour中动态分配FireGun的脚本仍在“查看”霰弹枪子弹的脚本,而不是新枪。