Burgzerg Arcade Hack and Slash Tutorial 24: NullReferenceExeption

I am following along with Burgzerg Arcade’s Unity 3D hack and slash tutorials. After I added _toon.StatUpdate to my CharacterGenerator.cs Update() function, I encountered this error when I run the character setup menu:

NullReferenceException: Object reference not set to an instance of an object
ModifiedStat.CalculateModValue () (at Assets/Scripts/Character Classes/ModifiedStat.cs:20)
ModifiedStat.Update () (at Assets/Scripts/Character Classes/ModifiedStat.cs:28)
BaseCharacter.StatUpdate () (at Assets/Scripts/Character Classes/BaseCharacter.cs:128)
CharacterGenerator.Update () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:28)

When double-clicked, it points towards the line of code in my foreach loop.

private void CalculateModValue() {
	  _modValue = 0;
		if(_mods.Count > 0)
			foreach(ModifyingAttribute att in _mods)
				_modValue += (int)(att.attribute.AdjustedBaseValue * att.ratio);
}

My ModifyingAttribute struct in case you need it:

public struct ModifyingAttribute {
	public Attribute attribute;
	public float ratio;

	public ModifyingAttribute(Attribute att, float rat){
		attribute = att;
		ratio = rat;
	}
}

Please take a look at my code and send me back any mistakes that you find. If you need any more information about my code, please leave a comment and I will post more.

The tutorials can be found here.

Thanks in advance for any help!

-Cob

Edit:

Here Are my top 14 lines of code. They are followed by my CalculateModValue().

using System.Collections.Generic;

public class ModifiedStat : BaseStat{
	private List<ModifyingAttribute> _mods; //A list of Attributes that modify the stat
	private int _modValue; 					//The amount added to the base value from the modifiers

	public ModifiedStat() {
		_mods = new List<ModifyingAttribute>();
		_modValue = 0;
	}

	public void AddModifier( ModifyingAttribute mod) {
		_mods.Add (mod);
	}

Edit:

If I know which line the error is coming from, how can I narrow it down to a single variable/object on that line?

I don’t think that there is enough code here to debug this by sight.

Just looking here:

private void CalculateModValue() {
      _modValue = 0;
       if(_mods.Count > 0)
         foreach(ModifyingAttribute att in _mods)
          _modValue += (int)(att.attribute.AdjustedBaseValue * att.ratio);
}

First glance analysis:

  • _modValue appears to be a value type, so I’ll ignore it for now.
  • _mods could be a null ref, but your edit makes that appear unlikely, and the script would error out before reaching the foreach loop.
  • att could be a null reference, if you did something silly like adding null to _mods.
  • att.attribute could be a null reference, if you did something silly like passing null to an Attribute constructor call.
  • att.attribute.AdjustedBaseValue appears to be a value, but would error out if either if the references preceding it are null.

So I’d look for anything like the following

new Attribute(null, 1);

AddModifier(null);

attribute = null;

_mods.Add(null);

_mods *= null;*

Also be suspicious of failed casting via operators like as.
Immediate recommendation is to figure out which reference is null. You might use the MonoDebugger, Debug.Log(), or add some newlines in your script to see if line numbers get more specific. Once you know what’s null, you might go hunting down for whatever code initializes or sets those values.

He corrects that in 25