Well you don’t do anything wrong but it seems you have the wrong idea about how C# works ^^. C# is an imperative programming language and not a functional programming language.
A field initializer is essentially an expression that is executed once when the class is initialized (that’s why they are called field initializers).
public float AddedValue = Score.scoreValue / 10f;
can be broken up into two parts. The variable declaration
public float AddedValue
and the field initializer:
= Score.scoreValue / 10f;
The variable declaration actually tells the compiler that your class should have a variable named AddedValue and to be of type float. A variable is just a place in memory where this value is stored. A variable never does something on it’s own. It’s just memory. The field initializer on the other hand is an expression (executable code) which runs once even before the constructor of the class runs. So the expression just provides an initial default value for that variable. This line does not create a “function”.
In essence what you did is similar to this:
public class VignetteScriptLight : MonoBehaviour
public float AddedValue;
AddedValue = Score.scoreValue / 10f;
// [ ... ]
So your field initializer runs just once at the start and the resulting value is stored in the variable. When your scoreValue changes it has no effect on the AddedValue variable.
Note that C# has many different concepts which are meant to simplify / shorten the code to make our lives easier. In C# we can create properties. A property can be used similar to a variable but it’s actually a pair of methods: a get and/or a set method. Properties on their own do not have any memory. Instead when you read a property you actually execute the get method and get back whatever the get method returns. The get method is executed every time you read a property. Likewise when you assign a value to a property its set method will be called and the value that you assigned will be passed as the special implicit parameter “value”. A property does not need to define both methods, but at least one of them. Of course a property without a set method can not be assigned,only read.
So you could declare your “AddedValue” as a read only property like that:
public float AddedValue
return Score.scoreValue / 10f;
In C# 6 there’s now an even shorter form for readonly properties which should have a method body. It’s essentially a combination of the lambda operator
=> and the property declaration. By changing your line to
public float AddedValue => Score.scoreValue / 10f;
AddedValue will no longer be a variable, but a property that executes this expression every time it is read. Note that this new syntax can only be used when you have C#6 support. This highly depends on your Unity version, your target platform and your actual used scripting backend and .NET version.