- Home /
Question by
PoorterGrameren · Apr 25, 2017 at 01:50 PM ·
errorobject referenceinstancesobject-reference-error
How do I fix this error
The error is called :NullReferenceException: Object reference not set to an instance of an object CharacterStat.AddStatBonus (System.Collections.Generic.List`1 statBonuses) (at Assets/scripts/weapon/CharacterStat.cs:17) PlayerWeaponController.EquipWeapon (.Item ItemToEquip) (at Assets/scripts/weapon/PlayerWeaponController.cs:30) InventoryController.Update () (at Assets/scripts/weapon/InventoryController.cs:21)
using System.Collections.Generic;
using UnityEngine;
public class CharacterStat : MonoBehaviour
{
public List<Basestat> stats = new List<Basestat>();
void Start()
{
stats.Add(new Basestat(5, "power", "your power level."));
stats.Add(new Basestat(3, "vittality", "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));
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerWeaponController : MonoBehaviour {
public GameObject PlayerHand;
public GameObject EquipedWeapon { get; set; }
CharacterStat characterstats;
IWeapon equipedWeapon;
void Start()
{
characterstats = GetComponent<CharacterStat>();
}
public void EquipWeapon(Item ItemToEquip)
{
if (EquipedWeapon != null)
{
characterstats.RemoveStatBonus(EquipedWeapon.GetComponent<IWeapon>().Stats);
Destroy(PlayerHand.transform.GetChild(0).gameObject);
}
EquipedWeapon = (GameObject)Instantiate(Resources.Load<GameObject>("Weapons/" + ItemToEquip.ObjectSlug), PlayerHand.transform.position, PlayerHand.transform.rotation);
equipedWeapon = EquipedWeapon.GetComponent<IWeapon>();
EquipedWeapon.transform.SetParent(PlayerHand.transform);
characterstats.AddStatBonus(ItemToEquip.Stats);
Debug.Log(equipedWeapon.Stats[0].GetCalculatedStatValue());
}
public void PerformWeaponAttack()
{
equipedWeapon.PerformAttack();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryController : MonoBehaviour {
public PlayerWeaponController playerWeaponController;
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");
}
void Update()
{
if(Input.GetKeyDown(KeyCode.V))
{
playerWeaponController.EquipWeapon(sword);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Object Reference Error On Play Audio 1 Answer
Nullreference Exception with ScriptableObject 0 Answers
CAN SOMEONE FINALLY HELP ME! 3 Answers
FSM object reference not set to an instance of an object 0 Answers