Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity 2D(空引用异常)_C#_Unity3d - Fatal编程技术网

C# Unity 2D(空引用异常)

C# Unity 2D(空引用异常),c#,unity3d,C#,Unity3d,我正在制作一个基于超级箱子设计的游戏:玩家与箱子碰撞,它选择了一个随机武器。 问题是,当我再次使用另一支枪射击时,如果霰弹枪子弹处于活动状态,我会得到异常(很明显,我会在稍后使子弹消失,但即使子弹消失,问题也会发生) 我需要很快地按下这个键,这样才会发生异常,所以我假设只有当猎枪子弹处于活动状态时才会发生异常 例外情况: 玩家拿起猎枪 玩家射击子弹(子弹在测试中不会消失) 玩家拿起另一支枪 当bullet仍处于活动状态时,玩家再次按下control键以发射bullet=>异常 发生异常的行是

我正在制作一个基于超级箱子设计的游戏:玩家与箱子碰撞,它选择了一个随机武器。
问题是,当我再次使用另一支枪射击时,如果霰弹枪子弹处于活动状态,我会得到异常(很明显,我会在稍后使子弹消失,但即使子弹消失,问题也会发生)

我需要很快地按下这个键,这样才会发生异常,所以我假设只有当猎枪子弹处于活动状态时才会发生异常

例外情况:

  • 玩家拿起猎枪
  • 玩家射击子弹(子弹在测试中不会消失)
  • 玩家拿起另一支枪
  • 当bullet仍处于活动状态时,玩家再次按下control键以发射bullet=>异常
发生异常的行是此
Transform-camerattransform=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;
     }
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
使用制度;
公共级消防枪:单一行为
{
//用于持有用户当前持有的枪支
公共列表=新列表();
//接拾音器
公共PickupBx;
//持有武器用户持有的当前名称
私人弦式武器;
//这将得到子弹预制件
公共游戏对象公告;
公共游戏对象射击子弹预制;
//运动员的弹位
公开游戏对象位置;
公共游戏对象射击枪弹位置;
//球员
公共游戏对象玩家;
//这是将在场景中实例化的子弹(rpg/机枪)
新世公共游戏对象公告;
//这是用于将被实例化的霰弹枪子弹
新世公共游戏物体射击枪弹;
//用来每隔几秒钟射击一次
private bool ShottyShottyOnce=假;
void Start()
{
pbx=GetComponent();
}
//每帧调用一次更新
无效更新()
{
if(GetComponent().flipX==true)
{
bulletPosition.transform.position=新矢量2(-92,0);
}
else if(GetComponent().flipX==false)
{
bulletPosition.transform.position=新矢量2(92,0);
}
AddObjectToList(pbx.guns,guns);
checkWeaponType();
}
//此方法将在pbxguns列表中的游戏对象中循环(第一个参数是PickupBox脚本中的枪列表),(myList是此脚本中的列表)_
void AddObjectToList(列表pbxGunsList,列表myList)
{
//循环浏览pbxGunsList列表中的所有gaeobjects
foreach(游戏对象进入pbxGunsList)
{
//如果此脚本中的列表不包含其他脚本中的游戏对象
如果(!myList.Contains(go))
{
//清除列表以确保我们不会得到重复的,并且在一个元素中有武器
myList.Clear();
//把枪加到我们的名单上
myList.Add(go);
}
}
}
//检查玩家持有的武器
void checkWeaponType()
{
//通过枪循环并将字符串设置为玩家当前持有的武器
对于(int i=0;i