Null Reference Error

I am not sure why I get a null reference error when I try to display the base value of a primary attribute on my character sheet.
I get the error from the setter / getter which looks like this:

// Primary Attributes
public Attribute GetPrimaryAttribute(int index) {

	return _primaryAttributes[index];
}

The line that triggers the error is this:

// Set Strength
_attStrength.text = activePlayer.GetComponent<PlayerCharacter>().GetPrimaryAttribute(0).BaseValue.ToString();

The _attStrength variable is a UILabel that is initialized properly and verified using debugging. I can debug log the attribute
at that index when the application starts, so I’m not sure why it’s throwing a null reference error when I try to assign that
base value to the display text.

The only thing I can imagine is that the attributes are intialized on the BaseCharacter script from which the PlayerCharacter
script derives and maybe it’s not accessing them properly. However, intellisense works fine for the BaseValue when I type
that line, so I suspect that everything is linked up properly.

Any insight please?

Hard to tell as there’s not much code, but your guess seems to point into the right direction.

So my guess is:

Even if the PlayerCharacter script extends the BaseCharacter script they do not necessarily share the same values. If the values are initialized in e.g. Start() of the BaseCharacter class you won’t see them in PlayerCharacter unless you specifically assign them.

Here’s a nice post on that topic:

So bottom line is: If the values are only initialized during runtime, you won’t see them in the derived classes. If they are initialized right from the beginning and changed during runtime you should not have a problem.

Thank you.
Problem solved by changing the way the activePlayer is chosen.
I was just using the partyMember list iterator instead of actually finding the instances of the player objects.