2017-03-27 173 views
0

我正在製作一款基於超級箱箱設計的遊戲:玩家與箱子碰撞並選擇隨機武器。
問題是如果獵槍子彈是活躍的,而我用另一支槍再次射擊,我得到了異常(我明顯會讓子彈消失,但即使子彈消失,問題也會發生)。Unity 2D(無參考例外)

我需要非常快速地按下按鍵纔會發生這種情況,所以我認爲只有當shotgun項目符號處於活動狀態時纔會發生異常。

例外情形:

  • 玩家拿起獵槍
  • 球員火子彈(子彈不消失用於測試目的)
  • 玩家拿起一支槍
  • 而子彈依舊活躍,玩家按下再次控制火災子彈=>異常

發生異常的行是這個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; 
    } 
} 
+2

請提供例外日誌的完整內容。 – zwcloud

+0

幾個建議你的代碼:而不是每次用戶按下一個鍵時調用'Transform cameraTransform = player.transform;'在'Start()'註冊它並保存到一個私有變量。另外,也可以使用if(bulletInScene!= null){if(t.name.Contains(「Bullet1(Clone)」)){// SOMETHING}}'if(bulletInScene!= null && t。 name.Contains(「Bullet1(Clone)」){// SOMETHING}'這樣你的代碼將會更清晰和更容易閱讀。 – Kardux

+0

可能的重複[什麼是NullReferenceException,以及如何解決?] (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – FCin

回答

0

錯誤提示player未設置。從代碼來看,我認爲你要麼有預製,要麼將其設置在檢查員中,要麼另一個腳本應該設置它。

也許你正在使用不同的預製件,無論什麼原因,當獵槍子彈處於活動狀態時,你忘記在該預製件上分配player

或者腳本應該在FireGun中動態分配player MonoBehaviour仍然「看着」獵槍子彈的腳本而不是新槍。

+0

播放器設置好,我有另一個腳本,但它只是這個我認爲是問題。這是什麼錯誤消息說NullReferenceException:對象引用未設置爲對象的實例 FireGun.checkWeaponType ()(在Assets/Scripts/FireGun.cs:133) FireGun.Update()(在Assets/Scripts/FireGun.cs:58) – SubZero

+0

仍試圖用霰彈槍的普通子彈修復它的工作很好,但不是新的子彈不知道爲什麼。 – SubZero

+0

嘿傢伙,所以我完全修改它不使用foreach循環我找到了一個更好的解決方案,但仍然想知道爲什麼它不工作。但是如果在像今天這樣說了7個小時之後,你還不能發現問題,那麼我在編程方面就會遇到一個問題,你認爲嘗試新解決方案還是應該知道爲什麼它不起作用? – SubZero