How to make action points max a seralize field?

I want to make the ACTION_POINTS_MAX constant in the Unit class a seralized field (so not a constant anymore)

When I make it a private serialized int, I get this error

How can I fix this? The problem is when trying to assign the serialized variable, to the actionsPoint int.

1 Like

In C#, a field initializer cannot refer to other instance fields (quoted from C# specification). You have to set the actionPoints in Awake()

private void Awake()
{
    actionPoints = ACTION_POINTS_MAX;
}
3 Likes

This is exactly right. When ACTION_POINTS_MAX is a constant, it is evaluated at compile time, and when the compiler compiles the code, it actually changes the line

private int actionPoints = ACTION_POINTS_MAX;

to

private int actionPoints = 4;

It can do this because no matter what ACTION_POINTS_MAX becomes in the const declaration, it will be that same value for all instances of the Unit class.
A [SerializeField] variable will be unique for each instance of the Unit, so using the = to initialize the variable as it’s declared outside of the class simply won’t work.

3 Likes

This solves it, thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms