I was following this tutorial series (http://www.youtube.com/watch?v=kK1eIk4inAU&list=SPE5C2870574BF4B06) and typed everything the same way he did. code compiled just fine, but when i run it i get a null reference exception error. is this because i’m using unity4 or is there something wrong with my code?
this is my code:
//ModifyingAttribute _mods initialized above
private void CalculateModValue(){
_modValue = 0;
if(_mods.Count > 0)
foreach(ModifyingAttribute att in _mods)
_modValue += (int)(att.attribute.AdjustedBaseValue * att.ratio);
}
public struct ModifyingAttribute {
public Attribute attribute;
public float ratio;
Well, the first issue to take care of is the missing “{” after “if(_mods.Count>0)”. You also should put a complete set of brackets on the foreach loop. You can get away without them there but only if it remains one line of code. More than one requires the brackets. It’s always a good idea to include them even for a single line like that.
if(_mods.Count > 0)
{
foreach(ModifyingAttribute att in _mods)
{
_modValue += (int)(att.attribute.AdjustedBaseValue * att.ratio);
}
}