As in title, I’m having a weird problem. I want to have a private const variable storing the value of sqrt, however, I am not able to assign it. Writing
private const float temp =
error CS0133: The expression being
assigned to `MapData.temp’ must be
Why? It doesn’t make any sense.
Because Mathf.Sqrt(3.0f) is not a constant. If you set a const to an int or string then when compiled it is literally set to that when compiled. Mathf.Sqrt(3.0f) returns a calculated value that can not be set when const’s as fields are compiled.
You could use readonly so you can set the value in a constructor(or awake/start) and then it can not be changed. You could make a private set property and get the sameish result.
You can also look up the error messages and read WHY something isn’t working from MSDN.
From the documentation for CS0133:
A const variable cannot take as its
value an expression that is not
CS0133 on MSDN