I know its one of the most common things but I really cant wrap my head around this.
in my main character script, i have
public MeleeWeapon currentWeapon;
//in update
if (Input.GetMouseButtonDown(0))
{
currentWeapon.updateDamage();
Debug.Log("min damage=" + currentWeapon.MinDamageVariable);
Debug.Log("max damage=" + currentWeapon.MaxDamageVariable);
}
//in update
in the Inspector, I have currentWeapon set to the object currently being held in the scene (as like a test object)
MeleeWeapon looks like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeleeWeapon : MonoBehaviour
{
[HideInInspector] public int WeaponHandsUsed;
[HideInInspector] public string WeaponType;
[HideInInspector] public string WeaponDamageType;
public string WeaponName;
public int MinDamageBase;
public int MaxDamageBase;
[HideInInspector] public int MinDamageVariable;
[HideInInspector] public int MaxDamageVariable;
[HideInInspector] public int AllDamageVariable;
public List<StatBase> damage = new List<StatBase>();
private void Start()
{
damage.Add(new StatBase(MinDamageBase, "MinDamage", "Weapon Minimum Damage")); //[0]
damage.Add(new StatBase(MaxDamageBase, "MaxDamage", "Weapon Maximum Damage")); //[1]
damage.Add(new StatBase(0, "extra Damage", "Weapon Extra Damage (min/Max)")); //[2]
MinDamageVariable = damage[0].ReturnFinalStatValue();
MaxDamageVariable = damage[1].ReturnFinalStatValue();
AllDamageVariable = damage[3].ReturnFinalStatValue();
//updateDamage();
}
public void updateDamage()
{
MinDamageVariable = damage[0].ReturnFinalStatValue();
MaxDamageVariable = damage[1].ReturnFinalStatValue();
AllDamageVariable = damage[2].ReturnFinalStatValue();
MinDamageVariable += AllDamageVariable;
MaxDamageVariable += AllDamageVariable;
}
public string GetWeaponType(int typeWanted) //0 = actual type, 1 = damage type, 2 = how many hands
{
if (typeWanted == 0)
{
if (WeaponType != null)
{return WeaponType;}
else
{ return "Weapon Has No Type. Fix That"; }
}
if (typeWanted == 1)
{
if (WeaponDamageType != null)
{return WeaponDamageType;}
else
{ return "no Weapon Damage Type, Fix that"; }
}
else
{
if (WeaponHandsUsed == 1)
{ return "1"; }
else
{ return "2"; }
}
}
}
whenever I try to call to do anything with currentWeapon, i get the classic "object reference not set to an instance of an object. But I really dont understand this at its core. arn’t I pointing to the instance by putting the (weapon in this case) in the “currentWeapon” slot of the inspector? This concept doesn’t make sense to me and nearly all threads correct a specific case of this and it’s just confusing me more.
again I’m sorry I know this is probably the most brought up error.
The error should give you a complete stack trace, showing the specific line of code that is throwing the exception. You’ll want to look at the line number. Then, put a break point on that line, and run the debugger. Then you can inspect which value is null.
well it would say line 7 in this case (of the first script)
but if I take that line away it also happens at lines 9 and 10. Or any way I try to reference my currentWeapon
also i added a breakpoint and the debugger stops at this line, but then I cant continue? I havnt used the bebugger for anything but finding problem lines in a long time.
also also most threads I read about this say to change
public MeleeWeapon currentWeapon;
to
public MeleeWeapon currentWeapon = new MeleeWeapon();
You general approach sounds fine. You just need to figure out what’s null. To do that, you need to get used to using the debugger. Without knowing more, perhaps something is setting currentWeapon to null? Do you have any code that’s setting this to null? Does it have a value in the debugger?
Just because it’s telling you the error is on line 7, that might not be the root cause of the error. Consider this sample code:
If I run this code, I get the following full error in the Console:
NullReferenceException: Object reference not set to an instance of an object
BreakableObjectControllerBase.DoSomethingElse () (at Assets/Code/Controllers/Breakables/BreakableObjectControllerBase.cs:173)
BreakableObjectControllerBase.DoSomething () (at Assets/Code/Controllers/Breakables/BreakableObjectControllerBase.cs:168)
BreakableObjectControllerBase.Update () (at Assets/Code/Controllers/Breakables/BreakableObjectControllerBase.cs:163)
BreakableGlassController.Update () (at Assets/Code/Controllers/Breakables/BreakableGlassController.cs:20)
You can’t see the line numbers in my sample code here, but you can see the methods in which the errors were occurring. You need to look to the top of the stack to see where the error is really happening.
public List<StatBase> damage;
private void Start()
{
damage = new List<StatBase>();
no change in error
Nothing sets it to null, the only reference to it at all right now is the one call in the OP.
So right now on click leftclick it tries to run updateDamage() and must return null somewhere, and never gets to the debug logs that are there.
When I try to just put out a debug log (remove the call to updateDamage()) it gives me null exception for each debug call, then debug logs mindamage = 0, max damage = 0. I realized I wasn’t setting the values that are actually being called by the debug, but I fixed that with no change to the error. (check OP for updated meleeWeapon Start())
now includes
but now this makes even less sense to me. how can min and max damageVariable be 0? I should mention that I have set min/max damageBase in the inspector as 2 and 6.
I also realize in hindsite I did not include my statBase system which I am using with my list. But that works without errors for everything else I used it for (its basically a List of more Lists of Ints, and returnFinalStatValue just adds everything together)
Holy Hell. That makes perfect sense. I was checking mouseclick (and calling the equipped weapon) from my characterStat script instead of the controller. so any object that used character stats was trying to reference their weapon (of which they had none)
changed the call to my to my controller instead and theres no more errors!
unfortunately im still getting 0’s for my min/max damage so i’ll keep working on that. But I’ll mark my OP solved and “like” all responses trying to help. thankyou guys.