title: Always Null Reference Exception: Object reference not set to an instance of an object
body: <p>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?</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p>And here is the code for player weapon controller</p>
  
  <pre><code>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&lt;CharacterStats&gt;();
      }
  
      public void EquipWeapon(Item itemToEquip)
      {
          if (EquippedWeapon != null)
          {
  
              characterStats.RemoveStatBonus(EquippedWeapon.GetComponent&lt;IWeapon&gt;().Stats);
              Destroy(playerHand.transform.GetChild(0).gameObject);
          }
  
  
          EquippedWeapon = (GameObject)Instantiate(Resources.Load&lt;GameObject&gt;("Weapons/" + itemToEquip.ObjectSlug), 
              playerHand.transform.position, playerHand.transform.rotation);
          equippedWeapon = EquippedWeapon.GetComponent&lt;IWeapon&gt;();
          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();
      }
  
  }
  </code></pre>
  
  <p>and here is the code for inventory controller</p>
  
  <pre><code>using UnityEngine;
  using System.Collections.Generic;
  using UnityEngine.UI;
  
  public class InventoryController : MonoBehaviour {
      public PlayerWeaponController playerWeaponController;
      public List&lt;Item&gt; playerItems = new List&lt;Item&gt;();
      public Item sword;
  
      void Start()
      {
  
          playerWeaponController = GetComponent&lt;PlayerWeaponController&gt;();
          List&lt;BaseStat&gt; swordStats = new List&lt;BaseStat&gt;();
          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);
          }
      }
  }
  </code></pre>
  
  <p>and here is the item</p>
  
  <pre><code>using UnityEngine;
  using System.Collections.Generic;
  
  public class Item {
      public List&lt;BaseStat&gt; Stats { get; set; }
      public string ObjectSlug { get; set; }
  
      public Item(List&lt;BaseStat&gt; _Stats, string _ObjectSlug)
      {
          this.Stats = _Stats;
          this.ObjectSlug = _ObjectSlug;
      }
  }
  </code></pre>
  
  <p>this the basestat</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  
  
  public class BaseStat
  {
  
      public List&lt;StatBonus&gt; 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&lt;StatBonus&gt;();
          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=&gt; x.BonusValue == statBonus.BonusValue));
      }
  
      public int GetCalculatedStatValue()
      {
          this.FinalValue = 0;
          this.BaseAdditives.ForEach(x =&gt; this.FinalValue += x.BonusValue);
          this.FinalValue += BaseValue;
          return FinalValue;
      }
  
  }
  </code></pre>
  
  <p>and this characterstat</p>
  
  <pre><code>using UnityEngine;
  using System.Collections.Generic;
  
  public class CharacterStats: MonoBehaviour {
      public List&lt;BaseStat&gt; stats = new List&lt;BaseStat&gt;();
  
      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&lt;BaseStat&gt; statBonuses)
      {
          foreach(BaseStat statBonus in statBonuses)
          {
  
              stats.Find(x=&gt; x.StatName == statBonus.StatName).AddStatBonus(new StatBonus(statBonus.BaseValue));
          }
      }
  
  
  
      public void RemoveStatBonus(List&lt;BaseStat&gt; statBonuses)
      {
          foreach (BaseStat statBonus in statBonuses)
          {
  
              stats.Find(x =&gt; x.StatName == statBonus.StatName).RemoveStatBonus(new StatBonus(statBonus.BaseValue));
          }
      }
  
  }
  </code></pre>
  
  <p>Thanks in advance</p>
  
  <p>It solved, I'm not inserting sword script to sword object. sorry and thanks for all</p>