始终为空引用异常:对象引用未设置为对象的实例

时间:2018-01-03 04:36:28

标签: java c# unity3d nullreferenceexception

这么多关于这个问题的帖子。但我找不到如何解决它。谁能告诉我我的代码在哪里出错?

  

NullReferenceException:未将对象引用设置为对象的实例   PlayerWeaponController.EquipWeapon(.Item itemToEquip(atAssets / Scripts / PlayerWeaponController.cs:33)   InventoryController.Update((atAssets / Scripts / Inventory / InventoryController.cs:61)

以下是玩家武器控制器的代码

using UnityEngine;
using System.Collections;

public class PlayerWeaponController : MonoBehaviour {
    public GameObject playerHand;
    public GameObject EquippedWeapon { get; set; }

    IWeapon equippedWeapon;
    CharacterStats characterStats;

    void Start()
    {
        characterStats = GetComponent<CharacterStats>();
    }

    public void EquipWeapon(Item itemToEquip)
    {
        if (EquippedWeapon != null)
        {

            characterStats.RemoveStatBonus(EquippedWeapon.GetComponent<IWeapon>().Stats);
            Destroy(playerHand.transform.GetChild(0).gameObject);
        }


        EquippedWeapon = (GameObject)Instantiate(Resources.Load<GameObject>("Weapons/" + itemToEquip.ObjectSlug), 
            playerHand.transform.position, playerHand.transform.rotation);
        equippedWeapon = EquippedWeapon.GetComponent<IWeapon>();
        equippedWeapon.Stats = itemToEquip.Stats;
        EquippedWeapon.transform.SetParent(playerHand.transform);
        characterStats.AddStatBonus(itemToEquip.Stats);
        Debug.Log(equippedWeapon.Stats[0]);


    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.X))
            PerformWeaponAttack();
        //if (Input.GetKeyDown(KeyCode.Z))
            //PerformWeaponSpecialAttack();
    }

    public void PerformWeaponAttack()
    {
        equippedWeapon.PerformAttack();
    }

}

这是库存控制器的代码

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

public class InventoryController : MonoBehaviour {
    public PlayerWeaponController playerWeaponController;
    public List<Item> playerItems = new List<Item>();
    public Item sword;

    void Start()
    {

        playerWeaponController = GetComponent<PlayerWeaponController>();
        List<BaseStat> swordStats = new List<BaseStat>();
        swordStats.Add(new BaseStat(6, "Power", "Your power level."));
        sword = new Item(swordStats, "sword");
    }

    public void EquipItem(Item itemToEquip)
    {
        playerWeaponController.EquipWeapon(itemToEquip);
    } 


    void Update()
    {
        if(Input.GetKeyDown(KeyCode.V))
        {
            playerWeaponController.EquipWeapon(sword);
        }
    }
}

这是项目

using UnityEngine;
using System.Collections.Generic;

public class Item {
    public List<BaseStat> Stats { get; set; }
    public string ObjectSlug { get; set; }

    public Item(List<BaseStat> _Stats, string _ObjectSlug)
    {
        this.Stats = _Stats;
        this.ObjectSlug = _ObjectSlug;
    }
}

这是basestat

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;


public class BaseStat
{

    public List<StatBonus> BaseAdditives { get; set; }
    public int BaseValue { get; set; }
    public string StatName { get; set; }
    public string StatDescription { get; set; }
    public int FinalValue { get; set; }

    public BaseStat(int baseValue, string statName, string statDescription)
    {
        this.BaseAdditives = new List<StatBonus>();
        this.BaseValue = baseValue;
        this.StatName = statName;
        this.StatDescription = statDescription;
    }

    public void AddStatBonus(StatBonus statBonus)
    {

        this.BaseAdditives.Add(statBonus);
    }

    public void RemoveStatBonus(StatBonus statBonus)
    {
        this.BaseAdditives.Remove(BaseAdditives.Find(x=> x.BonusValue == statBonus.BonusValue));
    }

    public int GetCalculatedStatValue()
    {
        this.FinalValue = 0;
        this.BaseAdditives.ForEach(x => this.FinalValue += x.BonusValue);
        this.FinalValue += BaseValue;
        return FinalValue;
    }

}

和这个characterstat

using UnityEngine;
using System.Collections.Generic;

public class CharacterStats: MonoBehaviour {
    public List<BaseStat> stats = new List<BaseStat>();

    private void Start()
    {
        stats.Add(new BaseStat(4, "Power", "Your power level."));
        stats.Add(new BaseStat(2, "Vitality", "Your vitality level."));
    }


    public void AddStatBonus(List<BaseStat> statBonuses)
    {
        foreach(BaseStat statBonus in statBonuses)
        {

            stats.Find(x=> x.StatName == statBonus.StatName).AddStatBonus(new StatBonus(statBonus.BaseValue));
        }
    }



    public void RemoveStatBonus(List<BaseStat> statBonuses)
    {
        foreach (BaseStat statBonus in statBonuses)
        {

            stats.Find(x => x.StatName == statBonus.StatName).RemoveStatBonus(new StatBonus(statBonus.BaseValue));
        }
    }

}

提前致谢

它解决了,我没有将剑形脚本插入剑对象。对不起,谢谢大家

2 个答案:

答案 0 :(得分:0)

因此,尝试从传入的itemToEquip参数设置统计信息时失败了。这是由于它,或者它的统计属性为null

这似乎是在InventoryController中设置的,其中InventoryController.EquipItem被调用?具体来说,我们需要知道应该流入该方法的输入"Item"来自何处?

如果您还没有,请尝试引用itemToEquip.Stats列表中的特定元素。

例如:

equippedWeapon.Stats = itemToEquip.Stats[0];

OR(通过使用ElementAt扩展方法):

equippedWeapon.Stats = itemToEquip.Stats.ElementAt(0);

答案 1 :(得分:0)

您应该检查null参数的itemToEquip条件或精确错误投掷对象

if(itemToEquip!=null)
{
equippedWeapon.Stats = itemToEquip.Stats;
}