Static Member Class

Hello!

I have a generic data wrapping class called Combat:

public Class Combat{
public float damage;
public float maxHealth;
public float movement;
//other stuff
}

I have several different types of units (for example Infantry, Tank) that I want to have static Combat members:

public Class Infantry{
public static Combat combat;
//other stuff
}

I want to organize it this way because all Infantry share damage, maxHealth, movement, etc, and I want to wrap all of that common behaviour rather than listing lots of static floats and bools for every class.

As I’ve currently written it, I get a null reference exception when I try to access Infantry.combat. For example, in the Infantry Start() method if I just try print(Infantry.combat.damage) it throws a null reference.

Anyone know what I need to do to get this data wrapping static class to work?

Thanks in advance!

Just because Combat combat is static, doesn’t mean it automatically has any value.
You still have to initialize combat.

public static Combat combat = new Combat();

Then, you still have to assign values to combat.damage, combat.maxHealth, etc.

1 Like

That’s it. I was confused about what “static” implied for a member class. Thanks Brathnann!