0

So many thread about this problem. But I can't find how to fix it. Can anyone tell me where do I go wrong in my code?

NullReferenceException: Object reference not set to an instance of an object PlayerWeaponController.EquipWeapon (.Item itemToEquip(atAssets/Scripts/PlayerWeaponController.cs:33) InventoryController.Update ((atAssets/Scripts/Inventory/InventoryController.cs:61)

And here is the code for player weapon controller

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();
    }

}

and here is the code for inventory controller

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);
        }
    }
}

and here is the item

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;
    }
}

this the 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;
    }

}

and this 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));
        }
    }

}

Thanks in advance

It solved, I'm not inserting sword script to sword object. sorry and thanks for all

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
OctavianWR
  • 217
  • 1
  • 16

2 Answers2

0

So it's failing when trying to set the stats from the passed in itemToEquip parameter. This is due to it, or it's Stats property being null.

It appears this is being setup in the InventoryController, where is InventoryController.EquipItem being called from? Specifically we'll need to know where the input "Item" that is supposed to flow into that method comes from?

If you haven't already, please attempt to reference a specific element in the itemToEquip.Stats List.

For example:

equippedWeapon.Stats = itemToEquip.Stats[0];

OR (via use of ElementAt extention method):

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

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
iHazCode
  • 622
  • 4
  • 15
0

You should check null condition for itemToEquip parameter or the exact error throwing object.

if(itemToEquip!=null)
{
equippedWeapon.Stats = itemToEquip.Stats;
}
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234